-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path459.cpp
44 lines (44 loc) · 996 Bytes
/
459.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// better29ms.cpp
class Solution {
public:
bool repeatedSubstringPattern(string s) {
int n = s.size();
if (n <= 1)
return false;
vector<int> factors(1, 1);
for (int i = 2; i * i <= n; ++i)
if (n % i == 0) {
factors.push_back(i);
factors.push_back(n / i);
}
for (auto it : factors)
if (isRepeated(s, it))
return 1;
return 0;
}
bool isRepeated(string &s, int len) {
for (int i = len; i < s.size();)
for (int j = 0; j < len; ++j, ++i)
if (s[j] != s[i])
return 0;
return 1;
}
};
// simple35ms.cpp
class Solution2 {
public:
bool repeatedSubstringPattern(string s) {
int n = s.size();
for (int i = n / 2; i > 0; --i)
if (n % i == 0 && isRepeated(s, i))
return 1;
return 0;
}
bool isRepeated(string &s, int len) {
for (int i = len; i < s.size();)
for (int j = 0; j < len; ++j, ++i)
if (s[j] != s[i])
return 0;
return 1;
}
};