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. C#中的那些全局异常捕获

    1.WPF全局捕获异常     public partial class App : Application     {         public App()         {    // 在异 ...

  2. 解决python3 UnicodeEncodeError: 'gbk' codec can't encode character '\xXX' in position XX

    从网上抓了一些字节流,想打印出来结果发生了一下错误: UnicodeEncodeError: 'gbk' codec can't encode character '\xbb' in position ...

  3. ajax浅析---ScriptManagerProxy

    使用ScriptManagerProxy控件 在ASP.NET AJAX中,由于一个ASPX页面上只能有一个ScriptManager控件,所以在有母版页的情况下,如果需要在Master-Page和C ...

  4. Linux/CentOS配置:使用yum update更新时不升级内核的方法

    RedHat/CentOS/Fedora使用 yum update 更新时,默认会升级内核.但有些服务器硬件(特别是组装的机器)在升级内核后,新的内核可能会认不出某些硬件,要重新安装驱动,很麻烦.所以 ...

  5. Ubuntu 14 编译安装 PHP 5.4.45 + Nginx 1.4.7 + MySQL 5.6.26 笔记

    Ubuntu 14 编译安装 PHP 5.4.45 + Nginx  1.8.0/1.4.7 + MySQL 5.6.26 笔记,主要是给自己的PC机安装,非生产环境! 一.下载必要的源码 1.1.下 ...

  6. 【转】关于URL编码/javascript/js url 编码/url的三个js编码函数

    来源:http://www.cnblogs.com/huzi007/p/4174519.html 关于URL编码/javascript/js url 编码/url的三个js编码函数escape(),e ...

  7. PowerDesigner的使用(一)

    一. PowerDesigner 功能 1. 需求管理:记录需求,分析设计模型 2. 生成文档:生成HTML格式文档,方便沟通. 3. 影响度分析:模型之间连接起来,同步修改功能. 4. 数据映射:提 ...

  8. Android学习笔记(九)——布局和控件的自定义

    //此系列博文是<第一行Android代码>的学习笔记,如有错漏,欢迎指正! View是 Android中一种最基本的 UI组件,它可以在屏幕上绘制一块矩形区域,并能响应这块区域的各种事件 ...

  9. event相关

    event.button 事件属性可返回一个整数,指示当事件被触发时哪个鼠标按键被点击. event.keyCode  事件属性可返回一个整数,指示当事件被触发时哪个键盘按键被点击. <scri ...

  10. Android Studio日志工具的使用

    Android Studio的LogCat工具 Verbose:对应Log.v(),这个方法用于打印那些最为琐碎的信息,意义最小的日志信息.是Android日志里面级别最低的一种. Debug:对应L ...