459.Repeated Substring Pattern
Example 1:
Input: "abab"
Output: True
Explanation: It's the substring "ab" twice.
Example 2:
Input: "aba"
Output: False
Example 3:
Input: "abcabcabcabc"
Output: True
Explanation: It's the substring "abc" four times. (And the substring "abcabc" twice.)lass Solution {
int max_factor(int a, int b)
{
if(a<b) swap(a,b);
if(b==0) return a;
for(int i=2;i<=b;i++)
if(a%i==0 && b%i ==0 )return i;
return 1;
}
public:
bool repeatedSubstringPattern(string s) {
vector<int>alpha(26,0);
for(auto c:s)
++alpha[c-'a'];
int len=0;
for(auto i:alpha)
// {
len=max_factor(len,i);
// cout<< i<<" ";
// }
// cout<<endl;
// cout<< len << " "<<endl;
if(len == 1 )return false;
int one_size=s.length()/len;
// cout<<one_size<<endl;
for(int j=0;j<one_size;j++)
for(int i=1;i<len;i++)
if(s[i*one_size+j] !=s[j]) return false;
return true;
}
};Last updated