Repeated Substring Pattern
This commit is contained in:
@@ -12,6 +12,7 @@
|
||||
- [翻转字符串里的单词](./reverse_words_in_a_string.md)
|
||||
- [左旋转字符串](./reverse_left_words.md)
|
||||
- [KMP](./kmp.md)
|
||||
- [重复的子字符串](./repeated_substring_pattern.md)
|
||||
|
||||
# 经典代码
|
||||
|
||||
|
23
notes/src/repeated_substring_pattern.md
Normal file
23
notes/src/repeated_substring_pattern.md
Normal file
@@ -0,0 +1,23 @@
|
||||
# 重复的子字符串
|
||||
|
||||
[459. Repeated Substring Pattern](https://leetcode.com/problems/repeated-substring-pattern/)
|
||||
|
||||
给定一个非空的字符串,判断它是否可以由它的一个子串重复多次构成。给定的字符串只含有小写英文字母,并且长度不超过10000。
|
||||
|
||||
示例:
|
||||
|
||||
输入: "abab"
|
||||
|
||||
输出: True
|
||||
|
||||
解释: 可由子字符串 "ab" 重复两次构成。
|
||||
|
||||
## 方法一
|
||||
|
||||
将两个 s 拼接在一起,如果里面还出现一个 s 的话,就认为是由重复字符串构成的。
|
||||
|
||||
## 方法二
|
||||
|
||||
假设 s 是由 n 个 x 构成的,那么它的最长公共前后缀的长度是 `(n - 1) * len(x)` 。
|
||||
|
||||
也就是说 `len(s) % (len(s) - (n - 1) * len(x)) == 0`
|
Reference in New Issue
Block a user