@siddharthp538 -
If I am not wrong, then c is denoting the number of ‘ab’ sequences in original string.
Your example is very well correct. Your applied concept is correct for that category of test case. But again, that’s just one of the many categories.
You stated that " C here will act as both common diff as well as the first term." . This is wrong. This is a property of this particular string. The common difference actually is number of ‘b’ in the string, and since here number of ‘b’ is equal to number of ‘ab’, you see the equality.
Also, there are some tricky categories of test cases.
One of the categories of the test case are the ones with no ‘ab’ sequence in original string.
Consider string “ba” repeated twice, giving “baba”. Now your c is 0. So if your formula has a multiplication with c, it will give a result 0.
I ran your code for this test case-
Input
1
3 2
baa
Output
1
While I didn’t debug your code (too many variables, made it confusing to see which variable is doing what), I saw that your program prints 1 (if k=2) for strings like “baaa” “baaaaaaa” &etc. I think this will help you find the flaw in logic/code.
What I meant when I said there is an AP perspective, is that, look at the final string.
Let s=“aba” and k=3. Final string (say p) is p= “abaabaaba”
For the very first a in s. It is used to form 1 ab. Also, we can say that p is formed of “aba aba aba” (I inserted a space between repetitions intentionally for clarity). Now look at the starting a of all 3 “aba” and find number of ab subsequence using that particular ‘a’
For 'a' at index 0, ab seq= 3.
For 'a' at index 3, ab seq=2,
For 'a' at index 6, ab seq=1.
I meant that this is an forming an AP. Similarly then I look at ‘ab’ s formed by every ‘a’ present in the string, and sum them up to get the final answer. See that it covers all ‘a’ and hence every possible ‘ab’ is covered, giving correct answer. Some observation and playing with cases/random samples would yield common difference= Number of b in string s, and a= number of ab formed in s by that particular a. Then we can safely apply the AP sum formula to find the answer.