cswork / OAOpen practice workspace

AmazonOA #362

Count Num Ways

Estimated MediumJudge-ready

Topics (AI-labelled)StringsTwo Pointers

Reverse each contiguous window of length k, one at a time, and count the window positions for which the whole reversed string is strictly smaller than the original. Identical text at different positions is counted separately; windows whose reversal changes nothing are not counted.

Input

The input is a JSON array [s,k]. Original bounds: 2≤length in code points≤1000000, 1≤k≤min(length,20). The original statement does not restrict the character set; this site specifies Unicode scalar characters and lexicographic order by code point, allows whitespace, NUL, non-BMP and Unicode separator characters, and keeps the original characters in the JSON encoding.

Output

Output a single integer, the answer.

Example 1

Input:

["amazon",3]

Output:

1

Example 2

Input:

["ababa",2]

Output:

2

Example 3

Input:

["😀
\u0000",3]

Output:

1

Example notes

The outputs of the three public examples are 1; 2; and 1, checked by independent enumeration or direct simulation.

Editorial

Loading solution…