Sp3cTer's blog

By Sp3cTer, 14 months ago, In English

Hello, I am currently solving a question in toph.co.

In the solution I used a for loop of length 15 and got tle but in the same code I removed for loop and used string.substr() funtion and got accepted. Can someone explain why?

As far I know substr has O(length of sub string) complexity.

Time Limit Exceeds

int k=15;
for(int i=0;i<a;i++)
	{
	    string s1="";
	    for(int j=0;j<k;j++)
		s1.push_back(s[j+i]);
	    if(sel>s1)
		sel=s1;
	}

Full code link- Click Here

Accepted

int k=15;
for(int i=0;i<a;i++)
	{
	    string s1;
	   s1=s.substr(i,k);
	    if(sel>s1)
		sel=s1;
	}

Full code link — Click Here

Thanks for help.

Full text and comments »

  • Vote: I like it
  • 0
  • Vote: I do not like it