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. IDL 的读写

    read_ifc代码如下: Write_ifc代码分析如下: (1)将数字转换为字符串的函数. function ntoc,a return,string(a,format='(g0)') end ( ...

  2. jsp 获取cookie 的值的方法

    Cookie cookies[]=request.getCookies(); //读出用户硬盘上的Cookie,并将所有的Cookie放到一个cookie对象数组里面 Cookie sCookie=n ...

  3. List和ArrayList之间转换的例子

    package Test01; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public c ...

  4. NSTimer相关方法

    - (void)fire; //立即触发定时器 - (NSDate *)fireDate;//开始时间 - (void)setFireDate:(NSDate *)date;//设置fireData, ...

  5. Nested weights are bad for performance

    警告信息“Nested weights are bad for performance”的消除方法 原因分析:在布局进行嵌套使用时,父布局与子布局都使用了android:layout_weight,但 ...

  6. LoadRunner 录制cas 登陆脚本

    关于CAS 的概念,见链接:http://www.360doc.com/content/15/0204/17/21706453_446251626.shtml 需要增加4个关联函数 //Correla ...

  7. BootStrap中关于input-group的问题(未解决)

    ****************************************2016年1月6日 23:08******************************* 本来想实现的如下功能: 但 ...

  8. xml 解析 python

    1 综述 有很多种解析方法. (1)  DOM   缺点是:1 不能解析格式不正确或者不规则xml  2据说只能解析utf-8格式,非utf-8需要转码 与SAX比较,DOM典型的缺点是比较慢,消耗更 ...

  9. centos lvm常用命令

    # vgs -a  VG     #PV #LV #SN Attr   VSize  VFree  cinder   1   0   0 wz--n- 30.39g 30.39g  os       ...

  10. Mysql主从复制(基于Log)

    Master(主)操作 实验机:两台 IP:192.168.1.5      192.168.1.10 操作系统:Linux RedHat 6.5 Mysql版本:5.6.31 #  vim /etc ...