How to resolve TLE (TRAINSET)?

my solution-
time given -1 sec
solution time- 1.01 sec

Your solution’s complexity is O(n*s). Try solving it in O(n).
Hint: Do not iterate through all the char of string.

thanks for your suggestion.
i made changes such as using an unordered list and using .compare function yet the TLE is still there.
new solution-