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的更多相关文章

  1. Leetcode459.Repeated Substring Pattern重复的子字符串

    给定一个非空的字符串,判断它是否可以由它的一个子串重复多次构成.给定的字符串只含有小写英文字母,并且长度不超过10000. 示例 1: 输入: "abab" 输出: True 解释 ...

  2. LeetCode 459. 重复的子字符串(Repeated Substring Pattern)

    459. 重复的子字符串 459. Repeated Substring Pattern 题目描述 给定一个非空的字符串,判断它是否可以由它的一个子串重复多次构成.给定的字符串只含有小写英文字母,并且 ...

  3. 43. leetcode 459. Repeated Substring Pattern

    459. Repeated Substring Pattern Given a non-empty string check if it can be constructed by taking a ...

  4. 459. Repeated Substring Pattern【easy】

    459. Repeated Substring Pattern[easy] Given a non-empty string check if it can be constructed by tak ...

  5. LeetCode_459. Repeated Substring Pattern

    459. Repeated Substring Pattern Easy Given a non-empty string check if it can be constructed by taki ...

  6. *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 ...

  7. 459. Repeated Substring Pattern

    https://leetcode.com/problems/repeated-substring-pattern/#/description Given a non-empty string chec ...

  8. [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 ...

  9. [LeetCode] Repeated Substring Pattern 重复子字符串模式

    Given a non-empty string check if it can be constructed by taking a substring of it and appending mu ...

随机推荐

  1. inline-block 前世今生(转)

    曾几何时,display:inline-block 已经深入「大街小巷」,随处可见 「display:inline-block; *display:inline; *zoom:1; 」这样的代码.如今 ...

  2. PhantomJS 基础及示例

    腾讯云技术社区-掘金主页持续为大家呈现云计算技术文章,欢迎大家关注! 作者:link 概述 PhantomJS is a headless WebKit scriptable with a JavaS ...

  3. 如何用css做一个爱心

    摘要:HTML的标签都比较简单,入门非常的迅速,但是CSS是一个需要我们深度挖掘的东西,里面的很多样式属性掌握几个常用的便可以实现很好看的效果,下面我便教大家如何用CSS做一个爱心. 前期预备知识: ...

  4. Chrome DevTools 代码覆盖率功能详解

    共 1812 字,读完需 3 分钟.工欲善其事必先利其器,前端周刊本周起每周会加餐 1 篇工具技巧,里面辅以动图,让大家看完就能学会,并上手使用.本文会介绍 Chrome Canary 新增的代码覆盖 ...

  5. 获取配置文件中的key的value

    获取App.config文件的key的value System.Configuration.ConfigurationManager.AppSettings["keyName"] ...

  6. http://zhidao.baidu.com/link?url=3tJ_i5gyYLrd7rFPk0eRYre_oxjCZvTOMOutp89LGhUgi6Ic6Ncama_GMAHnwfF73SVYGqy364vDfv6AY4ERPa

    http://zhidao.baidu.com/link?url=3tJ_i5gyYLrd7rFPk0eRYre_oxjCZvTOMOutp89LGhUgi6Ic6Ncama_GMAHnwfF73SV ...

  7. http://blog.csdn.net/rosten/article/details/17068285

    http://blog.csdn.net/rosten/article/details/17068285

  8. 如何让Firefox清除它记下的网站登录名和密码

  9. gdb调试多进程和多线程命令(转)

    1. 默认设置下,在调试多进程程序时GDB只会调试主进程.但是GDB(>V7.0)支持多进程的分别以及同时调试,换句话说,GDB可以同时调试多个程序.只需要设置follow-fork-mode( ...

  10. 压测过程中,CPU和内存占用率很高,案例简单分析

    Q:  最近公司测试一个接口,数据库采用Mongo    并发策略:并发400个用户,每3秒加载5个用户,持续运行30分钟    数据量:8000条左右 压测结果发现:    TPS始终在5左右   ...