Quote:How do optimise this loop for O(n) or O(nlogn)
Rule of seasoned programmers: first, make it right, then make it fast.
Quote:for example abcdeabb the answer for m = 1
There is no way the answer is 1 !
The example have 2 matches
First match is abcdeabb
and second match is abcdeabb
To get answer = 1
the match is abcdeabb
and this is second
occurrence of b.
Your algorithm is correct if a letter appear no more than 2 times is string.
Instead of checking a letter with remainder of string, my first approach would be to check a letter with beginning of string to ensure that a letter is checked with first occurrence.
I don't think your goal performance is possible.