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 ...
随机推荐
- 生成随机位数的UUID
1,生成UUID package com.jeeplus.common.utils; import java.util.UUID; /** * 生成唯一的UUID * * @author songya ...
- Mybatis通过ID查询 && 通过name模糊查询
接上篇:Mybatis环境搭建 在搭建环境时已经有了mapper和sqlMapConfig 1,数据库建表 prompt PL/SQL Developer import file prompt Cre ...
- Services
*在实际运行中同样的Service的确只能有一个. Services有两种启动形式: Started:其他组件调用startService()方法启动一个Service.一旦启动,Service将一直 ...
- ios-真机调试出错信息
更新证书错误Code Sign error: Provisioning profile ‘XXXX'can't be found 在Xcode中当你在更新了你得证书 ...
- OpenDigg - 挖掘优质开源项目库
OpenDigg - 挖掘优质开源项目库 OpenDigg专注于挖掘优质的开源项目库,通过技术和人工将软件项目分类整理,同时辅助简要的编译,方便广大程序员便捷地找到需要的开源项目. OpenDigg刚 ...
- JavaScript 的闭包用于什么场景
本文翻译自 MDN ( Mozilla Developer Network ): 原文地址:MDN 译文地址:shixinzhang 的博客 读完本文你将了解到: 词法作用域 闭包 闭包实战场景之回调 ...
- D3.js系列——比例尺和坐标轴
比例尺是 D3 中很重要的一个概念.绘制图形时直接用数值的大小来代表像素不是一种好方法,本章正是要解决此问题. 一.为什么需要比例尺 上一章制作了一个柱形图,当时有一个数组,绘图时,直接使用 250 ...
- win10下将spark的程序提交给远程集群中运行
一,开发环境: 操作系统:win19 64位 IDE:IntelliJ IDEA JDK:1.8 scala:scala-2.10.6 集群:linux上cdh集群,其中spark为1.5.2,had ...
- Kubernetes概念介绍和v1版本部署过程
简介: k8s一个开源的,跨主机管理容器应用集群的编排系统,为应用提供了基础的部署.维护和扩缩容机制. 编排:跨Docker主机同一管理容器集群. 目的 简化开发和运维容器集群的工作. 让开发和运维能 ...
- hbase shell删除键不听使唤
用Xshell登陆linux主机后,在hbase shell下死活不能使用backspace和delete删除误输的指令,只得不停退出,重登,仔细输..又错了,再退出,再登,仔细输...又错了...又 ...