I was trying to solve this problem. I've found a solution whose complexity is O(N^4) (N = length of string). Since N might be 100, I've thought that it's not fast enough. Then I looked at the official solution, surprisingly, it is almost same with mine. Can someone explain the complexity of this algorithm and tell why it isn't TLE?
If N is at most 100, then O(N4) is fine, since 1004 = 100M.
As a rule of thumb, try to remember the approximate limits for different complexities...
Surely, it will depend on the time limit and the constant factor of your algorithm, but it's useful to remember those values.
Thank you!
Also you can precompute all string comparsions to reduce complexity to N^3.
Can you explain how we can precompute? Wouldn't it be O(N^4) too?