459. Repeated Substring Patterneasy
Posted abc_begin
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了459. Repeated Substring Patterneasy相关的知识,希望对你有一定的参考价值。
459. Repeated Substring Pattern【easy】
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.)
一开始理解为只有abcabc这种情况了,搞了一个错误代码
1 class Solution { 2 public: 3 bool repeatedSubstringPattern(string s) { 4 int len = s.size(); 5 if (len % 2) { 6 return false; 7 } 8 9 return s.substr(0, len / 2) == s.substr(len / 2); 10 } 11 };
但题意是说: constructed by taking a substring of it and appending multiple copies of the substring together,如果按上面错误解法,abcabc这种字符串是可以判断正确的,但是对于abcdabcdabcd这种字符串就无能为力了。
解法一:
1 class Solution { 2 public: 3 bool repeatedSubstringPattern(string str) { 4 string nextStr = str; 5 int len = str.length(); 6 if(len < 1) return false; 7 for(int i = 1; i <= len / 2; i++){ 8 if(len % i == 0){ 9 nextStr = leftShift(str, i); 10 if(nextStr == str) return true; 11 } 12 } 13 return false; 14 } 15 16 string leftShift(string &str, int l){ 17 string ret = str.substr(l); 18 ret += str.substr(0, l); 19 return ret; 20 } 21 };
参考了@shell32的解法。
对于每个小长度进行判断,可以被整个长度整除,说明该小长度有可能成为备选;下面就是如何通过这个备选来看是否可以由多个备选组成整个字符串了。这个解法我们就是用到了左移再合并字符串的方法。
解法二:
1 bool repeatedSubstringPattern(string str) { 2 int n = str.length(); 3 for (int i = 1; i <= n / 2; i++) 4 if (n % i == 0 && str.substr(i) == str.substr(0, n - i)) 5 return true; 6 return false; 7 }
参考了@StefanPochmann的解法。
解法一中判断:“如何通过这个备选来看是否可以由多个备选组成整个字符串了”的方法,解法二直接采用字符串区间的方法来判断。
解法三:
1 bool repeatedSubstringPattern(string str) 2 { 3 return (str + str).substr(1, str.size() * 2 - 2).find(str)!=-1; 4 }
这是一个大神解法,参考了@ Xianming.Chen的解法。
他的思路如下:
str + str means doubling, (str + str).substr(1, str.size() * 2 - 2) means removing the first char of the first half and the last char of the second half.
- If there is no pattern, both of the first copy and the second copy will be changed, so str will not be found in (str + str).substr(1, str.size() * 2 - 2).
- If there is a pattern, the first char of str can still be found in the first half, and the last char of str can also be found in the second half. Here is an example: abcabc is the original string, and (bcabc abcab) includes abcabc.
对于上面的情况1,例子如下:
abaabc
baabcabaab
可以发现,根本找不到
对于上面的情况2,例子如下:
abcabc
bcabcabcab
可以发现,能找到
另外补充一下,C++ string截取字符串的函数:
s.substr(pos, n):截取s中从pos开始(包括0)的n个字符的子串,并返回
s.substr(pos):截取s中从从pos开始(包括0)到末尾的所有字符的子串,并返回
以上是关于459. Repeated Substring Patterneasy的主要内容,如果未能解决你的问题,请参考以下文章
LeetCode:459. Repeated Substring Pattern
43. leetcode 459. Repeated Substring Pattern
459. Repeated Substring Pattern
459. Repeated Substring Pattern