Count Num Ways π
Amazon is working on a new hashing approach that takes in the original string and a seed number.
Engineers decided that the seed can be generated from the same input string by counting the number of times
a reverse of a substring of length k makes the new string lexicographically smaller.
You are deployed with the task of developing a service that takes in a string s and
an integer k, and returns the number of ways to reverse any substring of length k such that the resulting string is lexicographically smaller than the original string.
Note:
-
1. A substring is a contiguous sequence of characters within a string. For example, the string
"zon"is a substring of"amazon","zone", etc.but is not a substring of"zoin","zozo", etc. -
2. A string
ais lexicographically smaller than stringbifa[i] < b[i]at the first index whereaandbdiffer. For example,"amazon"is lexicographically smaller than"amozan".
Function Description:
-
1. Complete the function
countNumWaysin the editor. -
2.
countNumWayshas the following parameters:- a.
string s:the original string - b.
int k:the algorithm parameter
- a.
Returns:
int: the number of possible ways to perform the operation ensuring the given constraint
1Example 1

2Example 2
Constraints
Limits and guarantees your solution can rely on.
-
1. 2 <= |s| <= 106 -
2. 1 <= k <= min (|s|, 20)