459. Repeated Substring Pattern【easy】
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.
- 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 Pattern【easy】的更多相关文章
- 43. leetcode 459. Repeated Substring Pattern
459. Repeated Substring Pattern Given a non-empty string check if it can be constructed by taking a ...
- *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 ...
- 459. Repeated Substring Pattern
https://leetcode.com/problems/repeated-substring-pattern/#/description Given a non-empty string chec ...
- 【LeetCode】459. Repeated Substring Pattern 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历子串 日期 [LeetCode] 题目地址:ht ...
- 【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 ...
- [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 ...
- 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 ...
- KMP - LeetCode #459 Repeated Substring Pattern
复习一下KMP算法 KMP的主要思想是利用字符串自身的前缀后缀的对称性,来构建next数组,从而实现用接近O(N)的时间复杂度完成字符串的匹配 对于一个字符串str,next[j] = k 表示满足s ...
- 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 ...
随机推荐
- 【动态规划】Gym - 100507G - The Debut Album
一般思路的dp是用f(i,j,0)表示前i位最后有j个1的方案数,用f(i,j,1)表示前j位最后有j个2的方案数,j都是大于等于1的,然后比较容易转移. 但这题卡内存,就只能用f(i,j)表示前i位 ...
- 找出能被5或6整除,但是不能被两者同时整除的数 Exercise05_11
/** * @author 冰樱梦 * 时间:2018年下半年 * 题目:找出能被5或6整除,但是不能被两者同时整除的数 * */ public class Exercise05_11 { publi ...
- 支付宝签名验证实现-Delphi版
支付宝签名验证实现-Delphi版 首先介结下支付宝签名验证流程: 一 支付宝密钥生成 支付宝提供秘钥生成工具https://docs.open.alipay.com/291/105971/ 用此下 ...
- python3发送html格式的邮件
def send_mail(to_list, sub, content, attpath): me = "*******" + "<" + mail_us ...
- opengl中VAO,VBO,IBO用法小结(zz) 【转】
http://cowboy.1988.blog.163.com/blog/static/751057982014380251300/ opengl中VAO,VBO,IBO用法小结 这三个玩意全面取代旧 ...
- IntelliJ IDEA 控制台中文乱码
1. 预热 刚刚接触IntelliJ IDEA几天,在易用性方面的确比Eclipse好很多,比较智能,各种插件.工具都已经集成,和Mac OS X类似——开箱即用. 但是还是老大难问题——中文乱码,让 ...
- Android Volley框架的使用(四)图片的三级缓存策略(内存LruCache+磁盘DiskLruCache+网络Volley)
在开发安卓应用中避免不了要使用到网络图片,获取网络图片很简单,但是需要付出一定的代价——流量.对于少数的图片而言问题不大,但如果手机应用中包含大量的图片,这势必会耗费用户的一定流量,如果我们不加以处理 ...
- 微信小程序 - 上传图片纯前端(多张、单张)
演示如下 可能有些命名不太规范,到时改一下即可 点击从github拉取:图片上传示例
- IOS Appstore价格表
- Python程序员的10个常见错误
关于Python Python是一门解释性的,面向对象的,并具有动态语义的高级编程语言.它高级的内置数据结构,结合其动态类型和动态绑定的特性,使得它在快速应用程序开发(Rapid Applicatio ...