题目

题目链接

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.)

给定一个没有空格的的字符串,查看该字符串是否由其中的一个子串重复多次得到。给定的字符串中只有小写的英文字母,而且长度不超过10000.

例子 1:

输入: "abab"

输出: True

解释:这是由子串"ab"重复得到

例子 2:

输入: "aba"

输出: False

例子3 3:

输入: "abcabcabcabc"

输出: True

解释:这是由子串"abc"重复四次得到的,也可以认为是"abcabc重复两次得到的"

代码

1.O(n^2)解法

class Solution {
public:
bool repeatedSubstringPattern(string str) {
int m=str.size();
for(int p=1;p<m/2+1;p++)
for(int i=0;i<m-p;i++)
{
if(str[i]!=str[i+p]) break;
if(m%p==0&&i==m-p-1) return true;
}
return false;
}
};

2.O(n)解法

解法链接这是其他人post到讨论区的解法,基于KMP思想做的,时间复杂性比较好。KMP算法可以参考这个链接

class Solution {
public:
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;
}

LeetCode - 459. Repeated Substring Pattern - O(n)和O(n^2)两种思路 - KMP - (C++) - 解题报告的更多相关文章

  1. 43. leetcode 459. Repeated Substring Pattern

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

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

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

  3. KMP - LeetCode #459 Repeated Substring Pattern

    复习一下KMP算法 KMP的主要思想是利用字符串自身的前缀后缀的对称性,来构建next数组,从而实现用接近O(N)的时间复杂度完成字符串的匹配 对于一个字符串str,next[j] = k 表示满足s ...

  4. LeetCode 459 Repeated Substring Pattern

    Problem: Given a non-empty string check if it can be constructed by taking a substring of it and app ...

  5. 459. Repeated Substring Pattern【easy】

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

  6. 【LeetCode】459. Repeated Substring Pattern

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

  9. 【LeetCode】459. Repeated Substring Pattern 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历子串 日期 [LeetCode] 题目地址:ht ...

随机推荐

  1. Spring入门第二课:Spring配置Bean的细节

    1.配置bean的作用域: 通过配置scope属性可以bean的作用域,参数有 prototype.request.session.singleton. 1)singleton为单例,IoC容器只会创 ...

  2. Ext自定义控件 - 自学ExtJS

    本文所有思想表达均为个人意见,如有疑义请多批评,如有雷同不甚荣幸. 转载请注明出处:Nutk'Z http://www.cnblogs.com/nutkz/p/3448801.html 在用到ExtJ ...

  3. LAMP+Varnish的实现

    基于Keepalived+Varnish+Nginx实现的高可用LAMP架构 注意:各节点的时间需要同步(ntpdate ntp1.aliyun.com),关闭firewalld(systemctl ...

  4. 浏览器端用JS实现创建和下载图片

    问题场景 在前端很多的项目中,文件下载的需求很常见.尤其是通过JS生成文件内容,然后通过浏览器端执行下载的操作.如图片,Execl 等的导出功能.日前,项目中就遇到了这类需求,在浏览器端实现保存当前网 ...

  5. MySQL---正确使用索引、limit分页、执行计划、慢日志查询

    正确使用索引 数据库表中添加索引后确实会让查询速度起飞,但前提必须是正确的使用索引来查询,如果以错误的方式使用,则即使建立索引也会不奏效.即使建立索引,索引也不会生效: - like '%xx' se ...

  6. Scala的符号入门

    Spark是由Scala编写的.Spark作为一款十分易用高效的大数据框架使用越来越广泛,Scala也随之有更多的人去学习. 语言相通,相信有python.java基础的程序员学习Scala并没有太大 ...

  7. Python语言发展的关键时间节点

    1989年:Python想法的产生 1991年:发布最早的Python可用版本 2000年:发布Python2.0 2010年:发布Python2.x系列的最后一个版本,主版本号为2.7 2008年: ...

  8. Python学习手册之正则表达式和元字符

    在上一篇文章中,我们介绍了 Python 的数据封装.类方法.静态方法和属性函数,现在我们介绍 Python 的正则表达式和元字符.查看上一篇文章请点击:https://www.cnblogs.com ...

  9. DATA 转 16 进制

    // 转 16进制 编码 NSData *data = [NSData dataWithBytes:(const void *)dataOut length:(NSUInteger)dataOutMo ...

  10. 3329: Xorequ

    3329: Xorequ https://www.lydsy.com/JudgeOnline/problem.php?id=3329 分析: 因为a+b = a^b + ((a&b)<& ...