Can anybody tell any alternative way to extract substring from a string in an efficient way except substr
or
Can anybody point out a mistake so that i can change something in my code and get AC?
Something like this. What you want is to get the hash of any substring in quick (like O(1)) time, then you can just try every length, so the complexity would be O(N) to hash and O(1) per O(N) lengths, leading to O(N) in total.
It is a brute force solution. You could have optimised it. You need not take substrings so many times. Apart from that string mapping can be done in constant time using hashing
Now test cases have been changed. At the time of contest if you would have done it by applying substr in a little optimized way yours would have passed as well. Test cases were weak so brute force solutions also passed.
Don’t make vectors of lengths rather use loop only:
int len=s.size()
int k =( len-2) /2
for(int i=1;I<=k;i++)
{
string b1, b2, c1, c2;
b1=s.substr(0, i) ;
b2=s.substr(i, i) ;
// do c1 c2 as well and compare b1, b2 and c1, c2
}
Your code consumes some extra time in vector operations and substring can be made only 4 times. It would have been accepted.