題目: LeetCode - 459. Repeated Substring Pattern

題目說明

給一個字串 s,求 s 是否為某個子串的數倍串接組成。

解題思路

如果 s 滿足題目的條件,那麼將字串複製一次後,去除頭跟尾,一定能找到原本的字串。

參考解法

1
2
3
4
5
6
7
8
9
10
11
12
// fast IO
static auto __ = []()
{
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
return 0;
}();
class Solution {
public:
bool repeatedSubstringPattern(string s) { return (s + s).find(s, 1) != s.size(); }
};