Sponsored
Sponsored
Use these hints if you're stuck. Try solving on your own first.
There are at most 26 different lengths of the complete substrings: <code>k *1, k * 2, … k * 26</code>.****
For each length, we can use sliding window to count the frequency of each letter in the window.
We still need to check for all characters in the window that <code>abs(word[i] - word[i - 1]) <= 2</code>. We do this by maintaining the values of <code>abs(word[i] - word[i - 1])</code> in the sliding window dynamically in an ordered multiset or priority queue, so that we know the maximum value at each iteration.