The Count Complete Substrings interview question is a "Hard" string problem. A substring is "complete" if:
k times.Meesho asks this Sliding Window interview pattern to test your ability to decompose a problem with multiple orthogonal constraints. It evaluates if you can split the string into segments where the second condition (adjacent difference) holds, and then solve the "exactly k times" problem on each segment using a fixed-size sliding window.
The problem uses Divide and Conquer with Sliding Window.
<= 2.k times.m distinct characters in a complete substring, its length must be m * k.m (1 to 26).m, we use a sliding window of fixed size m * k and check if all characters in the window appear exactly k times.s = "igigee", k = 2
"igigee".m=1 (length 2): ig, gi, ig, ge, ee. Only "ee" has all chars appearing 2 times.m=2 (length 4): igig, gige, igee. Only "igig" is valid.m=3 (length 6): "igigee". Chars: i:2, g:2, e:2. Valid!
Total count: 3.k.When a problem has a constraint like "each character appears exactly k times," the search space for the length of the substring is limited to 1*k, 2*k, ..., 26*k. This is a huge optimization!