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. Attrib命令,可以让文件夹彻底的隐藏起来

    Attrib命令,可以让文件夹彻底的隐藏起来,就算是在文件夹选项中设置了显示隐藏文件夹,也无法显示出来的.只能通过路径访问的方式打开文件夹.如上图,就是attrib命令的隐藏文件夹和显示文件夹的两条命 ...

  2. POJ 3648 Wedding

    2-SAT,直接选择新娘一侧的比较难做,所以处理的时候选择新郎一侧的,最后反着输出就可以. A和B通奸的话,就建边 A->B'以及B->A’,表示 A在新郎一侧的话,B一定不在:B在新郎一 ...

  3. 破解JS加密:url unicode加密而已

    加密所在的地方:http://tool.chinaz.com/Tools/UrlCrypt.aspx?url=www.baidu.com 结果: http://%77%77%77%2E%62%61%6 ...

  4. MC34063中文资料及应用实例(转)

    源:http://blog.chinaunix.net/uid-26199686-id-3207838.html MC34063A(MC33063)芯片器件简介 该器件本身包含了DC/DC变换器所需要 ...

  5. [ERROR] Fatal error: Can't open and lock privilege tables: Table 'mysql.user' doesn't exist 160913 02:11:21 mysqld_safe mysqld from pid file /tmp/mysql.pid ended

    -- :: [Note] InnoDB: Renaming log file ./ib_logfile101 to ./ib_logfile0 -- :: [Warning] InnoDB: New ...

  6. hibernate---树状映射

    总公司--分公司1, 分公司2 分公司1: 分公司1下部门1, 分公司1下部门2 分公司2: Org.java: package com.bjsxt.hibernate; import java.ut ...

  7. 微信小程序tips集合:无法输入文字/随时查看页面/元素审查/点击事件/数据绑定

    1:编辑文档无法输入文字 出现这种情况一般是因为之前编辑的文档未保存,所有在其他文档输入的时候会自动输入到未保存的文档中,在文档暂时编辑完毕后要ctrl+s随手保存,不然会出现无法打字情况 2: 随时 ...

  8. Inside dependency property

    依赖属性的定义,分为3步(以PresentationFramework中的System.Windows.Controls.Button为例) 1.  声明依赖属性 public static read ...

  9. SQL Server--导入和导出向导

    作用:创建一个只需要最少的转换就可以快速导入或导出数据的包 操作步骤: 1 打开打入和导出向导方式 有四种方式 1)在SSMS(SQL Server Management Studio)中,右击目标数 ...

  10. artTemplate-3.0(与项目实际结合)

    引入artTemplate.js <script type="text/javascript" src="${ctx}/assets/plugins/artTemp ...