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. js改变表单的内容样式

    一.改变单个样式    var obj = document.getElementById("id");   obj.style.cssText = " display: ...

  2. mute

    mute - 必应词典 英[mjuːt] n.哑吧:沉默的人:[法律]拒绝答辩的被告人:鸟粪 adj.哑的:缄默无言的:(一时)说不出话的:(猎狗)不叫的 v.排泄:减弱…的声音:柔和…的色调 网络静 ...

  3. ionic创建工程中遇到异常、错误及解决方法

    1. 创建工程——download failed ionic start myApp tabs 遇到如下错误 Downloading--Failed! Error:Timeout of 25000ms ...

  4. maven 创建project

    ------------------------------maven3常用命令--------------------------- 1.常用命令 1)创建一个Project mvn archety ...

  5. C++ map与unordered_map

    map与unordered_map对比 map unordered_map 红黑树(非严格二叉平衡搜索树)实现 哈希表实现 有序 无序 -- 查找时间复杂度为O(1),非常快 空间消耗较大 空间消耗较 ...

  6. 计算Python运行时间

    可以调用datetime 或者 time库实现得到Python运行时间 方法1 import datetime start_t  = datetime.datetime.now() #运行大型代码 e ...

  7. f5源站获取http/https访问的真实源IP解决方案

    1.背景 F5负载均衡设备,很多场景下需要采用旁挂的方式部署.为了保证访问到源站的数据流的request和response的TCP路径一致,f5采用了snat机制.但是这样导致源站上看到的来源IP都是 ...

  8. 【Linux 进程】fork函数详解

    一.fork入门知识 一个进程,包括代码.数据和分配给进程的资源.fork()函数通过系统调用创建一个与原来进程几乎完全相同的进程,也就是两个进程可以做完全相同的事,但如果初始参数或者传入的变量不同, ...

  9. (转)css选择器及其优先级

    文章主要介绍什么是CSS选择器,CSS选择器的分类以及CSS选择器的优先级三部分内容,希望能够帮助到正在学习CSS的童鞋,有什么不足的地方欢迎大家批评指正. 一.什么是CSS选择器? CSS选择器又被 ...

  10. 解决python3 UnicodeEncodeError: 'gbk' codec can't encode character '\xXX' in position XX(转)

    原文地址:https://www.cnblogs.com/feng18/p/5646925.html 从网上抓了一些字节流,想打印出来结果发生了一下错误: UnicodeEncodeError: 'g ...