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

有repeat pattern的话整个string可以分成几个相同的部分,可以被某一个数整除。

先找出这个数,从str.length()/2开始。

如果某个数可以被整除,这个数就是repeat pattern的长度, str.length/这个数就是repeat pattern重复的次数。

如果次数 * repeat pattern等于原数组,就说明原数组是repeat string组成的string。

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

教训是不要被实习的内容所累。。。这个算法由于题目条件限制变得很简单。。。

												

Repeated Substring Pattern Leetcode的更多相关文章

  1. LeetCode 459. 重复的子字符串(Repeated Substring Pattern)

    459. 重复的子字符串 459. Repeated Substring Pattern 题目描述 给定一个非空的字符串,判断它是否可以由它的一个子串重复多次构成.给定的字符串只含有小写英文字母,并且 ...

  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

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

  4. 459. Repeated Substring Pattern【easy】

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

  5. 459. Repeated Substring Pattern

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

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

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

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

  8. LeetCode算法题-Repeated Substring Pattern(Java实现)

    这是悦乐书的第236次更新,第249篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第103题(顺位题号是459).给定非空字符串检查是否可以通过获取它的子字符串并将子字符 ...

  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. CentOS 单用户登录&命令行、图像界面

    如何单用户登录: 这是一个很简单的问题,以前没重视,每次linux服务器无法正常启动时,都找应急盘,想偷懒,反而浪费了时间. 今天备忘如下: 1.系统启动时,按光标键调出GRUB引导菜单. 2.选定一 ...

  2. we7调用模板如何区分栏目页与详细页

    <a href='/xsdt/0000-00-00-00.html?id=<%# Eval("ID")%>'> 0000-00-00-00.html传参数来 ...

  3. jQuery+CSS实现的图片滚动效果

    http://www.helloweba.com/view-blog-139.html

  4. FragmentActivity与Fragment两者交互方法简介(转)

    FragmentActivity与Fragment两者交互方法简介 分类: Fragment 2014-07-07 18:17 88人阅读 评论(0) 收藏 举报 在Android4.0后很多时候我们 ...

  5. PAT (Advanced Level) 1049. Counting Ones (30)

    数位DP.dp[i][j]表示i位,最高位为j的情况下总共有多少1. #include<iostream> #include<cstring> #include<cmat ...

  6. 让你的MyEclipse具有jquery自动提示

    想让你的MyEclipse支持Jquery的自动提示更简单一些,照下图完成即可:      照上面图示已经完成了Jquery自动提示的配置,此时spket已经有两种AJAX库的自动提示,通过右边的De ...

  7. 简单的java高斯模糊算法

    import java.awt.Color; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOEx ...

  8. 关于js的几道经典题(作用域、原型链等)自己做的

    1. function test() { var a = 1; setTimeout(function() { alert(a); a = 3; }, 1000); a = 2; setTimeout ...

  9. Bridge 设计模式

    原文:http://www.linkedkeeper.com/detail/blog.action?bid=26 You are here:  架构&实践 - 设计模式 Frank     2 ...

  10. BNU OJ 51003 BQG's Confusing Sequence

    二进制++高精度取模 #include<cstdio> #include<cstring> #include<algorithm> using namespace ...