How to check if index of a string is earlier visited or not?

in order to check if index of a string is earlier visited or not, i can use a bool array visited with all values initialized to false, but in a particular substring how would i check if array visited is 0 or 1 from index i to j ?..as adding another loop for this will increase complexity.
also please tell if my code till now to solve this problem is correct or not
here is my code-

Hello @pragyachoudhary1111,

  1. The logic you have applied is correct but it has two issues:
    1.1. You have to find maximum number of CB numbers. So, you should look for the smallest ones first.
    1.2. The stoi() would not work for numbers greater than the range of int.

  2. Yes, you have to use another for loop. But, it’s time complexity would be O(s.length) instead of O(s)

Modified Code:

Hope, this would help.
Give a like if you are satisfied.