Ad
  • Custom User Avatar

    Yes, but m is constant and equal to 5.

  • Custom User Avatar

    How exactly is that so? This runs in O(N) time, if N is the length of the string. Also, in O(N) space, as it must store the result somewhere, which is O(N). This is the optimal complexity for this problem.

    You may find solutions with better performance in practice, I agree, but not with better time complexity.