Please check my code

https://ide.codingblocks.com/s/72165
Pls check this code, it is passing 6 test cases but failing 3.

Someone please reply !!!

Hi Sagnik,

Please check your code once for this case:
input:
14
23574414416111

expected output :
8

It is showing time limit error, help me to optimize this code.

Hi Sagnik,

There are some mistakes in your code

  1. Don’t use std stoi(), as it will result in run-error for some cases. So you can write your own string_to_int() like this :

    long string_to_int(string s)
    {
    long i,j=0;
    i = 0;
    while(s[j] >= '0' && s[j] <= '9')
    {
    i = i * 10 + (s[j] - '0');
    j++;
    }
    return i;
    }

  2. In is_valid(), termination condition of the for loop should be i < r instead of i <= r .

  3. In main(),
    a.) first for loop should iterate from 1 to s.length() instead of 0 to s.length()-1 .
    b.) inner for loop should iterate 0 to s.length()-i .
    c.) then calculate the index end_of_substring = j+i
    d.) take the substring as string sub = s.substr(j, end_of_substring-j);
    e.) Modify the function calls in if check like this if(is_cb_number(sub) && is_valid(visited,j,e))
    f.) Modify the for loop inside if check like this for(int k=j;k<e;k++)

I listed all the mistakes of your code above, please update your code accordingly and do reply to this thread if you are still facing any issue in this problem.

Keep Coding :slight_smile:

1 Like

So finally I am able to solve this question. Thanks a lot, @sanjeetboora. I really appreciate the way you have listed out the modifications. Thanks again.

Hi Sagnik

If your doubt is resolved please mark it as resolved in your online course’s ask doubt section.

Thanks

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

Yes I have marked it resolved and given the feedback also.