question - https://www.interviewbit.com/problems/stringoholics/
my solution- https://ide.geeksforgeeks.org/jU5zVWM7C2
I am unable to thing the optimal solution if available than this.
Stringoholics time limit exceded error
checkout this blog
time complexity of substr in c++ is O(n)
which makes your solution’s time complexity O(n^2).
That’s why you are getting TLE.