Whats wrong with this approach and code?


#1

string rotatestring(string s,int n)
{
string temp=s.substr(0,n);
s=s+temp;
s.erase(s.begin(),s.begin()+n);

return s;

}
int findtime(string s)
{
string temp;
temp=rotatestring(s,1);
int time=1;
while(temp!=s)
{
temp = rotatestring(temp,time++);
}

return time;

}
int gcd(int a,int b)
{
if(b==0)
return a;
return gcd(b,a%b);
}
int LCM(vectorv)
{
int ans=v[0];
for(int i=1;i<v.size();i++)
{
ans = (((v[i]*ans))/(gcd(v[i],ans)));
}
return ans;
}
int Solution::solve(vector &A) {

vector<int>t;
for(int i=0;i<A.size();i++)
{
    t.push_back(findtime(A[i]));
}

return LCM(t);

}


#2

I have used the same idea but getting error in long test cases. I think this approach is correct.
Is there any better way to find lcm?