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: 暴力法Brute Force

解法2:KMP,Knuth-Morris-Pratt字符串查找算法(简称为KMP算法)可在一个主文本字符串S内查找一个词W的出现位置。

Java:  直接截取了重复验证

public class Solution {
public boolean repeatedSubstringPattern(String str) {
int n = str.length();
for(int i=n/2;i>=1;i--) {
if(n%i==0) {
int m = n/i;
String substring = str.substring(0,i);
StringBuilder sb = new StringBuilder();
for(int j=0;j<m;j++) {
sb.append(substring);
}
if(sb.toString().equals(str)) return true;
}
}
return false; }
}

  

Python:

class Solution(object):
def repeatedSubstringPattern(self, str):
"""
:type str: str
:rtype: bool
"""
size = len(str)
for x in range(1, size / 2 + 1):
if size % x:
continue
if str[:x] * (size / x) == str:
return True
return False

Python: KMP

class Solution(object):
def repeatedSubstringPattern(self, str):
"""
:type str: str
:rtype: bool
"""
size = len(str)
next = [0] * size
for i in range(1, size):
k = next[i - 1]
while str[i] != str[k] and k:
k = next[k - 1]
if str[i] == str[k]:
next[i] = k + 1
p = next[-1]
return p > 0 and size % (size - p) == 0

C++:

class Solution {
public:
bool repeatedSubstringPattern(string str) {
int n = str.size();
for (int i = n / 2; i >= 1; --i) {
if (n % i == 0) {
int c = n / i;
string t = "";
for (int j = 0; j < c; ++j) {
t += str.substr(0, i);
}
if (t == str) return true;
}
}
return false;
}
};

C++:  

class Solution {
public:
bool repeatedSubstringPattern(string str) {
int i = 1, j = 0, n = str.size();
vector<int> dp(n + 1, 0);
while (i < n) {
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] 28. Implement strStr() 实现strStr()函数

[LeetCode] 686. Repeated String Match

All LeetCode Questions List 题目汇总

[LeetCode] 459. Repeated Substring Pattern 重复子字符串模式的更多相关文章

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

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

  2. 43. leetcode 459. Repeated Substring Pattern

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

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

  4. KMP - LeetCode #459 Repeated Substring Pattern

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

  5. LeetCode - 459. Repeated Substring Pattern - O(n)和O(n^2)两种思路 - KMP - (C++) - 解题报告

    题目 题目链接 Given a non-empty string check if it can be constructed by taking a substring of it and appe ...

  6. 459. Repeated Substring Pattern【easy】

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

  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. mysql终止当前正在执行的sql语句

    show processlist;找出你要停止的语句然后找出你要终止的语句的idkill 248

  2. Kotlin注解深入解析与实例剖析

    在上一次https://www.cnblogs.com/webor2006/p/11522798.html中学习了Kotlin注解相关的东东,这次继续对Kotlin的注解继续学习: 注解也可以拥有自己 ...

  3. 移动平台前端开发总结(ios,Android)

    首先我们来看看webkit内核中的一些私有的meta标签,这些meta标签在开发webapp时起到非常重要的作用 <meta content="width=device-width; ...

  4. 读取txt写入excel

    import csv #实现的思想:首先从txt中读取所有的内容,NUM=1当做键,其他当做值,如果查找缺少a,b,c,d,e,f,g# 则NUM不会添加到字典中,然后通过所有的NUM和字典中的KEY ...

  5. [51 Nod 1584] 加权约数和

    题意 求∑i=1N∑j=1Nmax(i,j)⋅σ1(ij)\large \sum_{i=1}^N\sum_{j=1}^Nmax(i,j)\cdot\sigma_1(ij)i=1∑N​j=1∑N​max ...

  6. LightOJ - 1326 - Race(DP)

    链接: https://vjudge.net/problem/LightOJ-1326 题意: Disky and Sooma, two of the biggest mega minds of Ba ...

  7. [Javascript] Sort by multi factors

    For example, we have a 2D arrays; const arys = [ [], [], [] ]; We want to sort by the number first, ...

  8. 关于dword ptr 指令

    dword 双字 就是四个字节ptr pointer缩写 即指针[]里的数据是一个地址值,这个地址指向一个双字型数据比如mov eax, dword ptr [12345678] 把内存地址12345 ...

  9. fio 文件系统io 性能测试安装使用

    备注: 使用的是yum 进行的安装,大家可以使用源码编译安装(centos 7) 安装 yum install -y fio 命令行参数 fio-2.2.8 fio [options] [job op ...

  10. ShardingSphere初探1 --Sharding-JDBC

    Sharding-JDBC 引入maven依赖: <dependency> <groupId>org.apache.shardingsphere</groupId> ...