Code working for small test cases but error in large values can ayone help me out beacuse my solution is less than o(n*n)


#1

// This is my code please look at it i have provided the comments to…

#define mod 1000000007
long long int gcd(long long int a,long long int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}

long long int lcm(long long int a,long long int b)
{
return (a / gcd(a, b)) * b;
}

int Solution::solve(vector &A) {
long long int ans = 1;

//Calculating for every string in the array min operations to make it same 
//corresponding taking lcm of the min operations and my prev ans
//Test case [a,ababa,aba]
//operation 1  lcm = 1
// ,,       4  lcm = 4
// ,,       2  lcm = 2
// ans = 2;
for(int i = 0 ; i < A.size() ; i++){
    string s = A[i];
    long long int j = 1;
    long long int len = s.length();
    long long int oper = 0;
    string s1 = s;
    while(1){
        s1 = s1.substr(j)+s1.substr(0,j);
        oper++;
        if(s1==s){
            break;
        }
        j = (j+1)%len;
    }
    oper = oper%mod;
    ans = lcm (ans,oper);
    ans = ans%mod;
}
return (int)ans;

}