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这种情况了,搞了一个错误代码

 class Solution {
public:
bool repeatedSubstringPattern(string s) {
int len = s.size();
if (len % ) {
return false;
} return s.substr(, len / ) == s.substr(len / );
}
};

但题意是说: constructed by taking a substring of it and appending multiple copies of the substring together,如果按上面错误解法,abcabc这种字符串是可以判断正确的,但是对于abcdabcdabcd这种字符串就无能为力了。

解法一:

 class Solution {
public:
bool repeatedSubstringPattern(string str) {
string nextStr = str;
int len = str.length();
if(len < ) return false;
for(int i = ; i <= len / ; i++){
if(len % i == ){
nextStr = leftShift(str, i);
if(nextStr == str) return true;
}
}
return false;
} string leftShift(string &str, int l){
string ret = str.substr(l);
ret += str.substr(, l);
return ret;
}
};

参考了@shell32的解法。

对于每个小长度进行判断,可以被整个长度整除,说明该小长度有可能成为备选;下面就是如何通过这个备选来看是否可以由多个备选组成整个字符串了。这个解法我们就是用到了左移再合并字符串的方法。

解法二:

 bool repeatedSubstringPattern(string str) {
int n = str.length();
for (int i = ; i <= n / ; i++)
if (n % i == && str.substr(i) == str.substr(, n - i))
return true;
return false;
}

参考了@StefanPochmann的解法。

解法一中判断:“如何通过这个备选来看是否可以由多个备选组成整个字符串了”的方法,解法二直接采用字符串区间的方法来判断。

解法三:

 bool repeatedSubstringPattern(string str)
{
return (str + str).substr(, str.size() * - ).find(str)!=-;
}

这是一个大神解法,参考了@ 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.

  1. 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).
  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 Pattern【easy】的更多相关文章

  1. 43. leetcode 459. Repeated Substring Pattern

    459. Repeated Substring Pattern Given a non-empty string check if it can be constructed by taking a ...

  2. *459. Repeated Substring Pattern (O(n^2)) two pointers could be better?

    Given a non-empty string check if it can be constructed by taking a substring of it and appending mu ...

  3. 459. Repeated Substring Pattern

    https://leetcode.com/problems/repeated-substring-pattern/#/description Given a non-empty string chec ...

  4. 【LeetCode】459. Repeated Substring Pattern 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历子串 日期 [LeetCode] 题目地址:ht ...

  5. 【LeetCode】459. Repeated Substring Pattern

    Given a non-empty string check if it can be constructed by taking a substring of it and appending mu ...

  6. [LeetCode] 459. Repeated Substring Pattern 重复子字符串模式

    Given a non-empty string check if it can be constructed by taking a substring of it and appending mu ...

  7. LeetCode 459 Repeated Substring Pattern

    Problem: Given a non-empty string check if it can be constructed by taking a substring of it and app ...

  8. KMP - LeetCode #459 Repeated Substring Pattern

    复习一下KMP算法 KMP的主要思想是利用字符串自身的前缀后缀的对称性,来构建next数组,从而实现用接近O(N)的时间复杂度完成字符串的匹配 对于一个字符串str,next[j] = k 表示满足s ...

  9. LeetCode - 459. Repeated Substring Pattern - O(n)和O(n^2)两种思路 - KMP - (C++) - 解题报告

    题目 题目链接 Given a non-empty string check if it can be constructed by taking a substring of it and appe ...

随机推荐

  1. 购物平台webApp+服务后台开发

    服务器后台参考:Cloud BaaS 主站地址:http://cloudbaas.sinaapp.com/ 演示地址:http://jeebaas.sinaapp.com/ Mobile UI 框架: ...

  2. SD 一轮集训 day1 lose

    神TM有是结论题,我讨厌结论题mmp. 杨氏矩阵了解一下(建议去维基百科). 反正就是推柿子,使劲推,最后写起来有一点小麻烦,但是在草稿纸(然鹅我木有啊)上思路清晰的话还是没问题的. #include ...

  3. 【拓扑排序】【堆】CH Round #57 - Story of the OI Class 查错

    拓扑排序,要让字典序最小,所以把栈改成堆. #include<cstdio> #include<queue> #include<algorithm> using n ...

  4. hadoop map(分片)数量确定

    之前学习hadoop的时候,一直希望可以调试hadoop源码,可是一直没找到有效的方法,今天在调试矩阵乘法的时候发现了调试的方法,所以在这里记录下来. 1)事情的起因是想在一个Job里设置map的数量 ...

  5. Linux命令之 file命令

    该命令用来识别文件类型,也可用来辨别一些文件的编码格式.它是通过查看文件的头部信息来获取文件类型,而不是像Windows通过扩展名来确定文件类型的. 执行权限 :All User 指令所在路径:/us ...

  6. libCurl 简单使用

    curl easy  的使用步骤 curl_easy_init() curl_easy_setopt() curl_easy_perform() curl_easy_cleanup() ------- ...

  7. Queue 队列的用法

    队列是一种特殊的线性表,它只允许在表的前端进行删除操作,而在表的后端进行插入操作. LinkedList类实现了Queue接口,因此我们可以把LinkedList当成Queue来用. 以下实例演示了队 ...

  8. sqlserver用于统计表索引情况

    /*eg: --调用该过程实例 --1 创建临时表 IF OBJECT_ID('tempdb..#index_sql_text') IS NOT NULL DROP TABLE #index_sql_ ...

  9. crontab 写入文件目录

    一.crontab 目录 [root@next-cloud-server etc]# cd /var/spool/cron/ [root@next-cloud-server cron]# ls roo ...

  10. windowsclient开发--使用、屏蔽一些快捷键

    每一个windowsclient都有自己的一些快捷键,有的是windows系统提供的. 今天就要与大家分享一下.在windowsclient开发过程中对按键的处理. ESC按键 Duilib这个库中, ...