Ideas: big brother saw a solution to a problem, very clever.
First, str2 str1 and must be of the same configuration of the substring, but a different number of sub-strings only, so str1 + str2 == str2 + str1. If not satisfy the foregoing equation, it does not represent the same substring.
Next, assuming that the length m is configured str1, str2 length is n. Then the maximum length of the substring must be able to simultaneously divisible by m, n. In fact, the maximum length is gcd (m, n).
class Solution {
public:
string gcdOfStrings(string str1, string str2) {
int len1=str1.size(),len2=str2.size();
if(str2+str1!=str1+str2) return "";
return str1.substr(0,gcd(len1,len2));
}
int gcd(int a,int b)
{
if(b==0) return a;
return gcd(b,a%b);
}
};