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. sql去除重复语句

    转自芙蓉清秀的BLOG http://blog.sina.com.cn/liurongxiu1211  sql去除重复语句 (2012-06-15 15:00:01) sql 单表/多表查询去除重复记 ...

  2. VHS介绍

    Java.JS HTTP流传输(VHS)介绍“我怎么才能让我的视频和Video.js一起玩?”“ 这是我们在使用Vio.js时最常见的问题之一.这是个好问题. 如果有人检查了Vo.js的拷贝,他们的内 ...

  3. python中的命名元组namedtuple

    namedtuple是继承自tuple的子类.namedtuple创建一个和tuple类似的对象,而且对象拥有可访问的属性 可利用collections.namedtuple构建一个简单的类. fro ...

  4. 把图片上的文字转换成word文字?

    转换后的文字不是很如意,但是免费方便. 1.打开Office办公软件自带的OneNote工具.随便新建一个笔记页面,以方便我们接下来的操作. 2.插入图片.在菜单栏里点击[插入],选择插入[图片],找 ...

  5. harbor在centos7.4下面配置自签名证书(域名是端口映射)

    1.harbor安装,按常规安装. 注意事项,端口映射 端口要外网的与内网一至. 配置文件修改 vim docker-compose.yml proxy: image: vmware/nginx-ph ...

  6. 关于MySQL数据库的备份方案

    这里简单总结MySQL的备份分为3种:分为冷备份,逻辑备份,热备份. 1.冷备份: 一般主要用于非核心业务,这类业务一般都是允许业务中断的,冷备份的特点就是数度快,恢复时也最为简单.通常直接复物理文件 ...

  7. redis 简介,安装与部署

    NOSQL简介 NoSQL,泛指非关系型的数据库,NoSQL数据库的四大分类: 键值(Key-Value)存储数据库:这一类数据库主要会使用到一个哈希表,这个表中有一个特定的键和一个指针指向特定的数据 ...

  8. HRBUST 2310 Tree Painting(无向图欧拉路径的性质)

    Give you a tree, can you draw the tree with minimum strokes without overlapping? Noted that it is ok ...

  9. stark组件之过滤操作【模仿Django的admin】

    一.先看下django的admin是如何实现过滤操作 首先在配置类中顶一个list_filter的列表,把要过滤的字段作为元素写i进去就可以了 class testbook(admin.ModelAd ...

  10. 使用SqlBulkCopy批量插入数据,测试20万条用时5秒

    using System;using System.Collections.Generic;using System.Linq;using System.Text; using System.Data ...