Okay so where array stores the number of times that position has been pressed.
Since the string is needed to traversed once I initialized it with zero.
Then I made a brute force and kept going till that position which is mentioned in the p array.
To optimize this I made a same where array where I just recorded till where in the array has the button been pressed, before that part the array must be pressed that many times too.
So that’s why I traversed in reverse and that relation was derived using the suffix knowledge .
This was a bit in depth explanation that has been previously provided but I decided to explain how my thought process worked