Repeated-Substring-Pattern
第91天。
今天的题目是Repeated Substring Pattern:
Given a non-empty string check if it can be constructed by taking a substring of it and appending multiple copies of the substring together. You may assume the given string consists of lowercase English letters only and its length will not exceed 10000.
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.)
一开始没看清,以为只有重复一次的情况,后来发现还可以重复多次,这样的话就不得不多扫描几遍了,有点希尔排序的解法:
1 | bool repeatedSubstringPattern1(string s) { |
然后是在dicuss
中的利用kmp
的解法,但我还是没看懂为什么可以这样做。
1 | bool repeatedSubstringPattern(string str) { |