Problem:

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

Summary:

判断所给的字符串是否为某字符串重复两次或多次。

Solution:

1. 暴力法:首先通过所给字符串长度枚举出可以作为重复子串的字符串长度,在每个枚举中,分别判断是否满足题意。

 bool repeatedSubstringPattern(string str) {
int len = str.size();
for (int i = ; i <= len / ; i++) {
if (len % i == ) {
string sub = str.substr(, i); int j;
for (j = ; j < len / i; j++) {
if (sub != str.substr(i * j, i)) {
break;
}
} if (j == len / i)
return true;
}
} return false;
}

2. 简便运算:将两个所给字符串s1首尾相连组成新的字符串s2 = s1 + s1,将s2首位字符去掉变为字符串s3。

若s1为满足题意的字符串,s3中必存在s1。

参考:https://discuss.leetcode.com/topic/68206/easy-python-solution-with-explaination

 class Solution {
public:
bool repeatedSubstringPattern(string str) {
int len = str.size();
string newStr = str + str;
newStr = newStr.substr(, * len - ); return newStr.find(str) != -;
}
};

LeetCode 459 Repeated Substring Pattern的更多相关文章

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

  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. 459. Repeated Substring Pattern

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

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

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

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

  9. 【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 ...

随机推荐

  1. Html书写规范

    #cnblogs_post_body ol { padding-left: 0px; } body { line-height: 1.6; } body, th, td, button, input, ...

  2. ELK常见错误分析(转)

    ELK 常见错误处理   ELK 这里就不介绍了,如何安装请参考博客之前的文章.在这里感谢ttlsa团队,同时,我很荣幸能加入到ttlsa团队中,分享点滴,凉白开说发文章有红包,期待这篇群主能给多少红 ...

  3. MongoDB副本集学习(三):性能和优化相关

    Read Preferences/读写分离 有时候为了考虑应用程序的性能或响应性,为了提高读取操作的吞吐率,一个常见的措施就是进行读写分离,MongoDB副本集对读写分离的支持是通过Read Pref ...

  4. Python的Set和List的性能比较 + 两者之间的转换

    1.能用set 不用list ~$ python -m timeit -n 1000 "[x for x in range(1000) if x in range(500, 1500)]&q ...

  5. Javascript高级程序设计——面向对象之理解对象

    在面向对象语言中都有类的概念,通过类来创建具有属性和方法的对象.而ECMAScript中没有类的概念,ECMAScript中定义了对象:无需属性的集合,其属性值可以包含基本值.对象.或者函数. 在Ja ...

  6. 笔记(一):ES6所改良的javascript“缺陷”

    ES6笔记(一):ES6所改良的javascript“缺陷”   块级作用域 ES5没有块级作用域,只有全局作用域和函数作用域,由于这一点,变量的作用域甚广,所以一进入函数就要马上将它创建出来.这就造 ...

  7. leetcode150 Evaluate Reverse Polish Notation

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  8. call(),apply()和bind()

    三个函数都是Function对象自带的三个方法,主要作用是改变函数中this的指向. call() 语法 fun.call(thisArg[, arg1[, arg2[, ...]]]) 该方法可以传 ...

  9. PYTHON 迭代器

    可以走直接作用于for循环的对象统称为可迭代对象使用:Iterable 使用isinstance()判断一个对象是否是Iterable对象: from collections import Itera ...

  10. OpenCV进阶之路:一个简化的视频摘要程序

    一.前言 视频摘要又称视频浓缩,是对视频内容的一个简单概括,先通过运动目标分析,提取运动目标,然后对各个目标的运动轨迹进行分析,将不同的目标拼接到一个共同的背景场景中,并将它们以某种方式进行组合.视频 ...