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. java对象详解

    java对象及线程详解 内存布局 普通对象布局 数组的内存布局 内部类的内存布局 对象分解 对象头-mark word(8字节) 实例数据 对齐填充(可选) java锁分析 volatile关键字 v ...

  2. 解决sqlite删除数据后,文件大小不变问题 转载

    原文地址:http://blog.csdn.net/yangchun1213/article/details/7656146   说了这么多,没进主题,我的主题是给Sqlite在删除数据后擦屁股. 大 ...

  3. Android Studio打包:“APP_NAME" IS NOT TRANSLATED IN ZH, ZH_CN……..解决办法

    开始用Android Studio更新到2.0稳定版,调试的时候没啥问题,在打包的时候出现了"app_name" is not translated in zh, zh_CN….. ...

  4. 前端:微信支付和支付宝支付在pc端和h5页面中的应用

    1:h5微信支付 使用的是https://pay.weixin.qq.com/wiki/doc/api/index.html  中的 (1):公司需要首先要配置公众号微信支付地址和测试白名单(支付的时 ...

  5. cs-Panination

    ylbtech-Unitity: cs-Panination Pager.cs IPagingOption.cs IPagedList.cs PagingOption.cs PagedList.cs ...

  6. Yii2系列教程六:集成编辑器

    上一篇文章我们实现了简单的用户权限管理,至于更先进的RBAC,我后面会单独出一篇文章来说说.在这一篇文章当中,我主要想写的是在Yii2中集成一个编辑器,因为在我们的实际开发当中,一个简单的textar ...

  7. Android Volley框架的使用(四)图片的三级缓存策略(内存LruCache+磁盘DiskLruCache+网络Volley)

    在开发安卓应用中避免不了要使用到网络图片,获取网络图片很简单,但是需要付出一定的代价——流量.对于少数的图片而言问题不大,但如果手机应用中包含大量的图片,这势必会耗费用户的一定流量,如果我们不加以处理 ...

  8. Node.js meitulu图片批量下载爬虫1.02版

    以前版本需要先查看网页源码,然后肉眼找到图片数量和子目录,虽说不费事,但多少有点不方便. 于是修改了一下,用cheerio自己去找找到图片数量和子目录,只要修改页面地址就行了.至此社会又前进了一步. ...

  9. perl学习笔记——字符串和排序

    用index查找子字符串 查找子字符串在主字符串中的相对位置.如: $where=index($big,$small); 注意index是从0开始的,如果查找不到就会返回-1: 加入第三个参数来指定开 ...

  10. rails delegate机制

    Delegate是一种应用composite来代替extend的机制,可以有效地降低代码的耦合性. Rails 2.2增加了delegate方法,可以十分方便地实现delegate机制. 01.def ...