IBM | OA | Extraordinary Substring
Anonymous User
508

Extraordinary substring is one whose sum of the mapped values of each letter is divisible by its length. Given string, count its total number of non-empty extraordinary substrings.

StringMappedSumLengthIsDivisible
a111Yes
s771Yes
d221Yes
f331Yes
as1,782Yes
sd7,292No
df2,352No
asd1,7,2103No
sdf7,2,3123Yes
asdf1,7,2,3134No

There are 6 extraordinary substrings.

Function
countSubstrings has the following parameter(s):
string input_str: a string of length n

Returns
int: the number of non-empty extraordinary substrings

Constraints

  • 1 <= n <= 2000
  • All characters of input_str are lowercase English letters.

Sample Case 0
Input: bdh
Output: 4
Explanation:
The extraordinary substrings are 'b','d','h', and 'bdh'.

Sample Case 1
Input: abcd
Output: 6
Explanation:
The extraordinary substrings are 'a','b','c','d','ab', and 'cd'.

Comments (1)