https://leetcode.com/problems/repeated-substring-pattern/#/description

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.)
Sol 1:
 
Try all possible divisors.
 
Let's say input string can be divided into d parts equally. d is small or equal to the square root of n, and then we check if d and len(str)/ d parts  can be pieced together to the input string.
 
 
class Solution(object):
def repeatedSubstringPattern(self, s):
"""
:type s: str
:rtype: bool
""" # brute force, O(n*2) time
n = len(s)
d = 1
while d * d <= n:
if n % d == 0:
for m in {d, n/d}:
if m > 1 and m * s[:n/m] == s:
return True
d += 1
return False

Note:

1 We use a variable m to check if d and len(str)/d can be glued together to the input string. 

 
It is a must to make sure len(str)/d is a divider because the while loop only checks the first half of the string.
 
ex1. str = 'abababab'
 
m in { 2, 8/2=4 }
 
When m = 2:
    2 * 'ababab' == str   :)
 
When m = 4:
    4 * ‘ab’ == str   :)
 
 
 
ex2. str = 'aba'
 
m in {1, 3/1=3 }
 
When m = 1:
    1 * 'aba' == str    :)
 
When m = 3:
    3 * 'a'  != str       :(
 
 
 
 
 
That's the reason why len(s)/d should also be checked! Otherwise string like 'aba' will get the wrong answer. 
 
 
 
Sol 2:
 
If we double the string, then if the string should be in somewhere from the second char to the last char of the doubled-string. 
 
 
 
 
class Solution(object):
def repeatedSubstringPattern(self, s):
"""
:type s: str
:rtype: bool
""" if not s:
return False ss = (s + s)[1:-1]
return ss.find(s) != -1

Note:

1 ss.find(s) returns the beginning index of s in ss. If not found, then return -1. 

 
 
 

Basic idea:

  1. First char of input string is first char of repeated substring
  2. Last char of input string is last char of repeated substring
  3. Let S1 = S + S (where S in input string)
  4. Remove 1 and last char of S1. Let this be S2
  5. If S exists in S2 then return true else false
  6. Let i be index in S2 where S starts then repeated substring length i + 1 and repeated substring S[0: i+1]
 
 

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. 459. Repeated Substring Pattern【easy】

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

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

  4. [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 ...

  5. LeetCode 459 Repeated Substring Pattern

    Problem: Given a non-empty string check if it can be constructed by taking a substring of it and app ...

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

  7. KMP - LeetCode #459 Repeated Substring Pattern

    复习一下KMP算法 KMP的主要思想是利用字符串自身的前缀后缀的对称性,来构建next数组,从而实现用接近O(N)的时间复杂度完成字符串的匹配 对于一个字符串str,next[j] = k 表示满足s ...

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

  9. 459. Repeated Substring Pattern 判断数组是否由重复单元构成

    [抄题]: Given a non-empty string check if it can be constructed by taking a substring of it and append ...

随机推荐

  1. Forms.WebBrowser与Controls.WebBrowser区别

    Forms.WebBrowser与Controls.WebBrowser区别 Forms.WebBrowser有ScrollBarsEnabled 属性,即窗口滚动条,可以设置为false即可: Co ...

  2. CSS3 box-shadow实现纸张的曲线投影效果

    一般的投影效果,尤其通过CSS实现的投影效果(无论是CSS3,还是IE滤镜),都是直来直往的.纸张是有卷角的,其投影就是曲面的,如何使用CSS模拟出纸张的卷边曲线投影效果. <div class ...

  3. 织梦 百度sitemap制作教程

    一.新建一个sitemap.htm模板 登录dedecms后台,选择[模板]-[模板管理]-[默认模板管理] 点击最下面的[新建模板]新建一个模板,并复制下面这段代码进去(将代码中的域名改为自己的): ...

  4. vue项目中,localhost可以访问,IP无法访问的问题

    用http://localhost:8082/可以访问,但是换到ip就访问不了,127.0.0.1.0.0.0.0访问也可以,就ip不行 根源----在config里面的index.js里面的modu ...

  5. php中bootstrap框架.popover弹出框,鼠标移动到上面自动显示,离开自动消失

    <div rel="name"></div> <script> $(function(){//显示弹出框 $("[rel=name]& ...

  6. oracle中获取当前整点和上一个小时整点,日期类型

    select to_date(to_char(sysdate,'yyyy-mm-dd hh'),'yyyy-mm-dd hh:mi:ss') from dual;select to_date(to_c ...

  7. java序列化的认识(从多本书和多个博客中的总结)

    Serializable接口是java.io下的一个标记接口,一个类要被序列化必须实现这个接口.

  8. Codeforces Beta Round #35 (Div. 2)

    Codeforces Beta Round #35 (Div. 2) http://codeforces.com/contest/35 A 这场的输入输出是到文件中的,不是标准的输入输出...没注意看 ...

  9. JSON Extractor

    JMeter处理responses 的json 对于请求1返回的结果,处理以后作为请求2的参数,JMeter提供了JSON 提取器 比如 responses 返回: {"statusCode ...

  10. 宝塔Linux面板 概述

    安装要求: Python版本: 2.6/2.7(安装宝塔时会自动安装) 内存:128M以上,推荐512M以上(纯面板约占系统10M内存) 硬盘:100M以上可用硬盘空间(纯面板约占20M磁盘空间) 系 ...