LeetCode 459 Repeated Substring Pattern
Problem:
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.)
Summary:
判断所给的字符串是否为某字符串重复两次或多次。
Solution:
1. 暴力法:首先通过所给字符串长度枚举出可以作为重复子串的字符串长度,在每个枚举中,分别判断是否满足题意。
bool repeatedSubstringPattern(string str) {
int len = str.size();
for (int i = ; i <= len / ; i++) {
if (len % i == ) {
string sub = str.substr(, i);
int j;
for (j = ; j < len / i; j++) {
if (sub != str.substr(i * j, i)) {
break;
}
}
if (j == len / i)
return true;
}
}
return false;
}
2. 简便运算:将两个所给字符串s1首尾相连组成新的字符串s2 = s1 + s1,将s2首位字符去掉变为字符串s3。
若s1为满足题意的字符串,s3中必存在s1。
参考:https://discuss.leetcode.com/topic/68206/easy-python-solution-with-explaination
class Solution {
public:
bool repeatedSubstringPattern(string str) {
int len = str.size();
string newStr = str + str;
newStr = newStr.substr(, * len - );
return newStr.find(str) != -;
}
};
LeetCode 459 Repeated Substring Pattern的更多相关文章
- 43. leetcode 459. Repeated Substring Pattern
459. Repeated Substring Pattern Given a non-empty string check if it can be constructed by taking a ...
- [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 ...
- 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 ...
- 459. Repeated Substring Pattern【easy】
459. Repeated Substring Pattern[easy] Given a non-empty string check if it can be constructed by tak ...
- 459. Repeated Substring Pattern
https://leetcode.com/problems/repeated-substring-pattern/#/description Given a non-empty string chec ...
- *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 ...
- 【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 ...
随机推荐
- height:100%和height:auto的区别
一直不明白height:100%和height:auto的区别,最近在制作前端页面时都用了height:100%;overflow:hidden; ,可是有些浏览器出现莫名的奇妙的问题,但换成heig ...
- Jquery控制滚动显示欢迎字幕v2
Jquery控制滚动显示欢迎字幕v2: 之前做的那个比较适合测试环境,但要套入到网站中,有两个按钮在那摆着,还是不太好看.后面对代码进行了修改,如下: 参考代码: <html> <h ...
- 从一个弱引用导致的奔溃 谈 weak assign strong的应用场景【iOS开发教程】
从一个弱引用导致的奔溃 谈 weak assign strong的应用场景 .h中的定义方法一: @property (nonatomic, assign) NSArray *dataSource; ...
- R入门<二>-时间序列研究
续之前那篇随笔 前天写完随笔后,很自豪的拿出来去跟带我入数据挖掘和SAS基础的大牛@八公炫耀,然后收获了一堆时间序列的材料,非常感谢大牛! ARIMA就是看图形,ACF和PACF,原理不需要知道,因为 ...
- call() 和 apply() ----预定义的函数方法
- OS X Framework Library not loaded: 'Image not found'的解决办法
参考:OS X Framework Library not loaded: 'Image not found' 1.首先将相应的framework手动复制到/System/Library/Framew ...
- Redis学习笔记八:独立功能之二进制位数组
Redis 提供了 setbit.getbit.bitcount.bitop 四个命令用于处理二进制位数组. setbit 命令用于为位数组指定偏移量上的二进制位设置值,偏移量从 0 开始计数. ge ...
- .apache2 设置多个虚拟域名
<VirtualHost 127.0.0.2:80> ServerName www.xylilun.cn DocumentRoot E:/www/ylll <Directory E: ...
- vim编译运行java程序
想让你的vim可以编译运行java只要在你的配置文件里面加一句: map <F5> :!javac %&&java %:r <CR> 就可以编译运行java了
- Android隐藏标题栏和状态栏
一.隐藏标题栏 //隐藏标题栏 this.requestWindowFeature(Window.FEATURE_NO_TITLE); 二.隐藏状态栏 //隐藏状态栏 this.getWindow() ...