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.html
Example 1:java
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.)
这道题给了咱们一个字符串,问其是否能拆成n个重复的子串。那么既然能拆分红多个子串,那么每一个子串的长度确定不能大于原字符串长度的一半,那么咱们能够从原字符串长度的一半遍历到1,若是当前长度能被总长度整除,说明能够分红若干个子字符串,咱们将这些子字符串拼接起来看跟原字符串是否相等。 若是拆完了都不相等,返回false。app
解法一:post
class Solution { public: bool repeatedSubstringPattern(string str) { int n = str.size(); for (int i = n / 2; i >= 1; --i) { if (n % i == 0) { int c = n / i; string t = ""; for (int j = 0; j < c; ++j) { t += str.substr(0, i); } if (t == str) return true; } } return false; } };
下面这种方法是参考的网上的这个帖子,原做者说是用的KMP算法,LeetCode以前也有一道应用KMP算法来解的题Shortest Palindrome,可是感受那道题才是KMP算法。这道题也称为KMP算法感受怪怪的(关于KMP的详细介绍请参见从头至尾完全理解KMP,也能够看博主本身写的一篇KMP Algorithm 字符串匹配算法KMP小结),KMP算法中的next数组是找当前位置的最大相同前缀后缀的个数,而这道题维护的一位数组dp[i]表示,到位置i-1为止的重复字符串的字符个数,不包括被重复的那个字符串,什么意思呢,咱们举个例子,好比"abcabc"的dp数组为[0 0 0 0 1 2 3],dp数组长度要比原字符串长度多一个。那么咱们看最后一个位置数字为3,就表示重复的字符串的字符数有3个。若是是"abcabcabc",那么dp数组为[0 0 0 0 1 2 3 4 5 6],咱们发现最后一个数字为6,那么表示重复的字符串为“abcabc”,有6个字符。那么怎么经过最后一个数字来知道原字符串是否由重复的子字符串组成的呢,首先固然是最后一个数字不能为0,并且还要知足dp[n] % (n - dp[n]) == 0才行,由于n - dp[n]是一个子字符串的长度,那么重复字符串的长度和确定是一个子字符串的整数倍,参见代码以下:url
解法二:spa
class Solution { public: bool repeatedSubstringPattern(string str) { int i = 1, j = 0, n = str.size(); vector<int> dp(n + 1, 0); while (i < n) { if (str[i] == str[j]) dp[++i] = ++j; else if (j == 0) ++i; else j = dp[j]; } return dp[n] && (dp[n] % (n - dp[n]) == 0); } };
相似题目:.net
参考资料:
https://discuss.leetcode.com/topic/68498/one-line-with-regex/2
https://discuss.leetcode.com/topic/67992/java-simple-solution-with-explanation