LeetCode459. Repeated Substring Pattern
Description
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.)
解法一
思路:循环左移字符串k位与原来的字符串进行比较,(k位是由字符串长度len的因数决定)相等则为true。
如9因数有1,3,9,因为最大的substring为字符串长度的一半,所以k<=len2
class Solution {
public:
string move(string s, int l) {
string res = s.substr(l);
res += s.substr(0,l);
return res;
}
bool repeatedSubstringPattern(string s) {
int len = s.size();
string str;
for (int i = 1; i <= len/2; i++) {
if (len % i == 0) {
str = move(s, i);
if(str == s) return true;
}
}
return false;
}
};
Submission Details
107 / 107 test cases passed.
Status: Accepted
Runtime: 36 ms
解法二
用到KMP算法
1.Roughly speaking, dp[i+1] stores the maximum number of characters that the string is repeating itself up to position i.
2.Therefore, if a string repeats a length 5 substring 4 times, then the last entry would be of value 15.
3.To check if the string is repeating itself, we just need the last entry to be non-zero and str.size() to divide (str.size()-last entry).
bool repeatedSubstringPattern(string str) {
int i = 1, j = 0, n = str.size();
vector<int> dp(n+1,0);
while( i < str.size() ){
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;
}
LeetCode459. Repeated Substring Pattern的更多相关文章
- Leetcode459.Repeated Substring Pattern重复的子字符串
给定一个非空的字符串,判断它是否可以由它的一个子串重复多次构成.给定的字符串只含有小写英文字母,并且长度不超过10000. 示例 1: 输入: "abab" 输出: True 解释 ...
- LeetCode 459. 重复的子字符串(Repeated Substring Pattern)
459. 重复的子字符串 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 ...
- 459. Repeated Substring Pattern【easy】
459. Repeated Substring Pattern[easy] Given a non-empty string check if it can be constructed by tak ...
- LeetCode_459. Repeated Substring Pattern
459. Repeated Substring Pattern Easy Given a non-empty string check if it can be constructed by taki ...
- *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 ...
- [Swift]LeetCode459. 重复的子字符串 | Repeated Substring Pattern
Given a non-empty string check if it can be constructed by taking a substring of it and appending mu ...
- [LeetCode] Repeated Substring Pattern 重复子字符串模式
Given a non-empty string check if it can be constructed by taking a substring of it and appending mu ...
随机推荐
- IOS,苹果内购和添加广告
内购——应用内购买 通过苹果应用程序商店有三种主要赚钱的方式: 直接收费(与国内大部分用户的消费习惯相悖) 广告(降低用户体验 应用程序名称带Lite可以添加广告) O2O -> Online推 ...
- binlog监听工具-canal
官网 https://github.com/alibaba/canal/wiki
- C#中yield return用法
转载:http://www.jb51.net/article/54810.htm http://www.cnblogs.com/HunterWei/archive/2012/06/13/csharpy ...
- spring融合activitymq-all启动报错的解决办法
报错信息: nested exception is java.lang.NoSuchMethodError: org.springframework.core.annotation.Annotated ...
- 如何查看oracle的sid
1.怎样查看Oracle的数据库名称sid 用sysdba身份登录 比如 conn sys/密码 as sysdba 匿名管理员登陆 执行 select name form V$database; ...
- Intel haxm安装失败问题解决
在安装Intel haxm为安卓模拟器加速时,会遇到提示VT-X未开启问题,问题提示如下图 工具/原料 Intel haxm 安卓模拟器 方法/步骤 确认你的处理器是否是Intel的,如果是A ...
- 特征选取方法PCA与LDA
一.主成分分析(PCA)介绍 什么是主成分分析? 主成分分析是一种用于连续属性降维的方法,把多指标转化为少数几个综合指标. 它构造了原始属性的一个正交变换,将一组可能相关的变量转化为一组不相关的变 ...
- Spark(八) -- 使用Intellij Idea搭建Spark开发环境
Intellij Idea下载地址: 官方下载 选择右下角的Community Edition版本下载安装即可 本文中使用的是windows系统 环境为: jdk1.6.0_45 scala2.10. ...
- 最短路径——Floyd,Dijkstra(王道)
题目描述: 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最短的从商店到赛场的路线 ...
- 【DP】【单调队列】【NOI2005】瑰丽华尔兹
340. [NOI2005] 瑰丽华尔兹 ★★★ 输入文件:adv1900.in 输出文件:adv1900.out 简单对照 时间限制:1 s 内存限制:128 MB [任务描写叙述] 你跳过华尔兹吗 ...