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. JavaScript中 window.parent 、window.top、window.self代表的含义

    在应用有frameset或者iframe的页面时,parent是父窗口,top是最顶级父窗口(有的窗口中套了好几层frameset或者iframe),self是当前窗口, opener是用open方法 ...

  2. ngCordova插件安装使用

    为什么ngCordova ngCordova是在Cordova Api基础上封装的一系列开源的AngularJs服务和扩展,让开发者可以方便的在HybridApp开发中调用设备能力,即可以在Angul ...

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

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

  4. [AngularJS] jQuery时代

    抹平浏览器差异的jQuery出现了 jQuery有什么 jQuery使得开发无刷新动态页面(AJAX)或者单页应用(SAP)变得 相当简单. 标准的HTML页面是静态的,被浏览器渲染后就产生了一个DO ...

  5. ajax验证登录注册

    <form id="form1" onsubmit="return false;"> <table id="login-table& ...

  6. 剑指Offer 替换空格

    题目描述 请实现一个函数,将一个字符串中的空格替换成“%20”.例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy.   思路: 替换空格,先遍历一遍记 ...

  7. JDBC的应用

    JDBC的开发步骤: 1.  引入JDBC驱动架包 2.  程序中引入JDBC驱动类     3.  创建java与数据库的连接 4.  跟数据库交互:发送sql语句,接收数据库对sql语句的执行结果 ...

  8. tornado 非阻塞方法

    http://sebastiandahlgren.se/2014/06/27/running-a-method-as-a-background-thread-in-python/

  9. Union-Find Algorithm

    Union-Find Algrithm is used to check whether two components are connected or not. Examples: By using ...

  10. WinForm开发框架--动态读取DLL模式

    1\ WinForm开发框架--动态读取DLL模式   http://www.2cto.com/kf/201306/217199.html 2\ 广州爱奇迪     http://www.iqidi. ...