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. C# Guid用法

    GUID(全局统一标识符)是指在一台机器上生成的数字,它保证对在同一时空 中的所有机器都是唯一的.通常平台会提供生成GUID的API.生成算法很有意思,用到了以太网卡地址.纳秒级时间.芯片ID码和许多 ...

  2. OpenGL学习-------点、直线、多边形

    上一课中,我们学习了如何绘制几何图形,但大家如果多写几个程序,就会发现其实还是有些郁闷之处.例如:点太小,难以看清楚:直线也太细,不舒服:或者想画虚线,但不知道方法只能用许多短直线,甚至用点组合而成. ...

  3. java.lang.RuntimeException: java.lang.NoSuchMethodException:

    [java] 15/12/19 14:09:46 INFO mapred.JobClient: Task Id : attempt_201512182036_0017_m_000000_0, Stat ...

  4. 笔记整理--HTTP Header 详解

    HTTP Header 详解 2013/09/21 | 分类: IT技术 | 0 条评论 | 标签: HTTP 分享到:36 原文出处: zcmhi HTTP(HyperTextTransferPro ...

  5. BZOJ 3110:[Zjoi2013]K大数查询(整体二分)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3110 题意:-- 思路:其实和之前POJ那道题差不多,只不过是换成区间更新,而且是第k大不是第k小, ...

  6. read cache return null

    http://stackoverflow.com/questions/8063590/unexpected-behaviour-after-memcached-server-restarts-how- ...

  7. 使用myeclipse新建和删除web项目时一定要小心

    今天使用myeclipse在非工作空间的路径下新建一个web项目,路径内包含了原有一些web项目,可是我在指定位置时,在那个路径下多写一个子目录的路径,以为myeclipse会 为我默认新建子目录以便 ...

  8. sping 对 hibernate进行事务管理--Annotation, xml, 大多数使用XML

    1. UserServiceTest.java: package com.bjsxt.service; import org.junit.Test; import org.springframewor ...

  9. 控制流之while

    while语句只要在一个条件为真的情况下,while语句允许你重复执行一块语句.while语句是所谓 循环 语句的一个例子.while语句有一个可选的else从句.使用while语句~~~~~~~~~ ...

  10. SQL复习一(基础知识)

    1.什么是sql SQL(structure query language)是结构化查询语言,它是对关系型数据库的操作语言.它可以应用到所有的数据库中,例如:MySQL.Oracle.SQL serv ...