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 ...
随机推荐
- [HTML/CSS]margin属性用法
概述 在一些小的项目中,前台样式还是需要自己来写的,这时候,margin在布局中还是有一定的地位的.上篇文章中介绍的盒子模型中,就有margin的存在. margin margin可以用来设置css块 ...
- 《西安交大电路》(Principles of Electrical Circuits) 学习笔记
内容简介:电路分析是电子类专业的第一门基础课. 电路理论包括电路分析和电路综合两大方面内容.电路分析的主要内容是指在给定电路结构.元件参数的条件下,求取由输入(激励)所产生的输出(响应):电路综合则主 ...
- 解决小米手机Android Studio安装app 报错的问题It is possible that this issue is resolved by uninstalling an existi
问题描述 Android Studio升级到2.3版本之后,小米手机MIUI8不能运行Android Studio程序,报如下错误: Installation failed with message ...
- git新建和删除远程分支
创建远程分支: 新建本地分支 git checkout -b branch_name 推送到远程分支,分支名字和本地分支名字相同 git push origin branch_name:branch_ ...
- 基于CentOS与VmwareStation10搭建Oracle11G RAC 64集群环境:2.搭建环境-2.9. 配置用户等效性(可选项)
2.9.配置用户等效性(可选项) Oracle 11g r2 ssh也可以在安装过程中配置. 2.9.1. grid用户等效性 1.以下均以grid用户执行: 在两个节点的grid主目录分别创建.ss ...
- Linux Bash严重漏洞修复方法
日前Linux官方内置Bash中新发现一个非常严重安全漏洞,黑客可以利用该Bash漏洞完全控制目标系统并发起攻击,为了避免Linux服务器受影响,就要尽快修补该漏洞了.(漏洞参考https://acc ...
- CSS3 transform变换
CSS3 transform变换 1.translate(x,y) 设置盒子位移2.scale(x,y) 设置盒子缩放3.rotate(deg) 设置盒子旋转4.skew(x-angle,y-angl ...
- HAWQ技术解析(十八) —— 问题排查
(原文地址:http://hawq.incubator.apache.org/docs/userguide/2.1.0.0-incubating/troubleshooting/Troubleshoo ...
- java源码阅读System
1类签名与注释 public final class System System类包含一些有用的类属性和方法.该类不能被实例化,所以其所有属性与方法都是static的. 2标准输入输出流 public ...
- 淘宝JAVA中间件Diamond
以下是转载自网上资料,但是根据步骤可以搭建出diamond配置中心服务器. 项目中需要用到diamond的理由是, 项目中使用了很多定时任务和异步任务.而且这些定时任务和异步任务都是分布式的安排在多个 ...