I have been trying to figure out the Big O Complexity of this code:
void findSection(string s)
{
string start = "abc";
string end = "xyz";
int startIndex = s.find(start);
string tempS;
while(startIndex<s.size())
{
for(int i=startIndex; i<=s.size()-3; i+=3)
{
tempS = s.substr(i,3);
if(tempS==end)
{
return;
}
}
startIndex = s.find(start,startIndex+3);
}
}
Worse case would be if:
s="abcabcabcabc.......
Orignally I though the complexity would be O(nlog(n)), but after looking at this link: Time Complexity of this double loop
I came up with the complexity as:
i=0 ——> 0,3,6,9,…,b b
i=3 ——> 3,6,9,…,b b-3
i=6 ——> 6,9,…,b b-6
i=9 ——> 9,…,b b-9
i=b ——> b-b
b + (b-3) + (b-6) + ... + (b-b) = _____
And now I am thinking it might be O(n^2) but I am not sure. Thank you for your time and help.