Skip to content

Commit 9b243cd

Browse files
committed
0459 solved.
1 parent 37e18d4 commit 9b243cd

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.25)
2+
project(cpp_0459)
3+
4+
set(CMAKE_CXX_STANDARD 17)
5+
6+
add_executable(cpp_0459 main.cpp)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/// Source : https://leetcode.com/problems/repeated-substring-pattern/description/
2+
/// Author : liuyubobobo
3+
/// Time : 2023-08-21
4+
5+
#include <iostream>
6+
7+
using namespace std;
8+
9+
10+
/// Brute Force
11+
/// Time Complexity: O(sqrt(n) * n)
12+
/// Space Complexity: O(n)
13+
class Solution {
14+
public:
15+
bool repeatedSubstringPattern(string s) {
16+
17+
int n = s.size();
18+
if(n == 1) return false;
19+
for(int i = 1; i * i <= n; i ++){
20+
if(n % i) continue;
21+
22+
if(ok(n, s, i)) return true;
23+
24+
if(i * i == n || i == 1) continue;
25+
if(ok(n, s, n / i)) return true;
26+
}
27+
return false;
28+
}
29+
30+
private:
31+
bool ok(int n, const string& s, int len){
32+
string subs = s.substr(0, len);
33+
for(int i = len; i <= n - len; i += len)
34+
if(s.substr(i, len) != subs) return false;
35+
return true;
36+
}
37+
};
38+
39+
40+
int main() {
41+
42+
return 0;
43+
}

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ email: [[email protected]](mailto:[email protected])
493493
| 456 | [132 Pattern](https://leetcode.com/problems/132-pattern/) | [solution](https://leetcode.com/problems/132-pattern/solution/) <br/> [缺:O(n) 单调栈算法] | [C++](0001-0500/0456-132-Pattern/cpp-0456/) | | |
494494
| 457 | [Circular Array Loop](https://leetcode.com/problems/circular-array-loop/) | [] | [C++](0001-0500/0457-Circular-Array-Loop/cpp-0457/) | | |
495495
| 458 | [Poor Pigs](https://leetcode.com/problems/poor-pigs/) | [solution](https://leetcode.com/problems/poor-pigs/solution/) | [C++](0001-0500/0458-Poor-Pigs/) | | |
496-
| | | | | | |
496+
| 459 | [Repeated Substring Pattern](https://leetcode.com/problems/repeated-substring-pattern/description/) | [] | [C++](0001-0500/0459-Repeated-Substring-Pattern/cpp-0459/) | | |
497497
| 460 | [LFU Cache](https://leetcode.com/problems/lfu-cache/description/) | [solution](https://leetcode.com/problems/lfu-cache/solutions/2815229/lfu-cache/?orderBy=most_votes) | [C++](0001-0500/0460-LFU-Cache/) | | |
498498
| 461 | [Hamming Distance](https://leetcode.com/problems/hamming-distance/) | [solution](https://leetcode.com/problems/hamming-distance/solution/) | [C++](0001-0500/0461-Hamming-Distance/cpp-0461/) | | |
499499
| 462 | [Minimum Moves to Equal Array Elements II](https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/) | [solution](https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/solution/) | [C++](0001-0500/0462-Minimum-Moves-to-Equal-Array-Elements-II/cpp-0462/) | | |

0 commit comments

Comments
 (0)