459. Repeated Substring Pattern
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.)
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.
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:
- First char of input string is first char of repeated substring
- Last char of input string is last char of repeated substring
- Let S1 = S + S (where S in input string)
- Remove 1 and last char of S1. Let this be S2
- If S exists in S2 then return true else false
- 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的更多相关文章
- 43. leetcode 459. Repeated Substring Pattern
459. Repeated Substring Pattern Given a non-empty string check if it can be constructed by taking a ...
- 459. Repeated Substring Pattern【easy】
459. Repeated Substring Pattern[easy] Given a non-empty string check if it can be constructed by tak ...
- *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 ...
- [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 ...
- 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 ...
- 【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 ...
- KMP - LeetCode #459 Repeated Substring Pattern
复习一下KMP算法 KMP的主要思想是利用字符串自身的前缀后缀的对称性,来构建next数组,从而实现用接近O(N)的时间复杂度完成字符串的匹配 对于一个字符串str,next[j] = k 表示满足s ...
- 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 ...
- 459. Repeated Substring Pattern 判断数组是否由重复单元构成
[抄题]: Given a non-empty string check if it can be constructed by taking a substring of it and append ...
随机推荐
- phacon只能访问index action
location / { if (!-e $request_filename) { rewrite ^(.*)$ /index.php?_url=$1 last; break; } }
- centos 7 redis-4.0.11 哨兵
redis-master:192.168.199.223 redis-slave_1: 192.168.199.224 redis-slave_2: 192.168.199.252 redis-mas ...
- 全国高校绿色计算大赛 预赛第一阶段(C++)第2关:扔桃子
挑战任务 动物园有一只小猴子喜欢吃桃子,不过它有个很独特的习惯,每次都把找到的桃子分成相等的两份,吃掉一份,留一份.如果不能等分,小猴子就会丢掉一个然后再分.第二天再继续这个过程,直到最后剩一个桃子了 ...
- SystemC_Basic
1.http://baike.baidu.com/view/1018980.htm 百度百科介绍的很好,举例很清晰. 2.SystemC的三个基本进程:SC_METHOD,SC_THREAD,SC_C ...
- Example of Formalising a Grammar for use with Lex & Yacc
Here is a sample of a data-file that we want to try and recognise. It is a list of students and info ...
- 153. Find Minimum in Rotated Sorted Array (Array; Divide-and-Conquer)
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...
- Android系统显示原理
Android的显示过程可以概括为:Android应用程序把经过测量.布局.绘制后的surface缓存数据,通过SurfaceFlinger把数据渲染到屏幕上,通过Android的刷新机制来刷新数据. ...
- setTimeout设置为0的意义
今天再看 Promise 代码时,有个地方用到了setTimeOut函数,但是第2个参数设为0,顿时懵逼了,这是啥意思? function resolve(newValue) { value = ne ...
- Requested a new session but one was in progress
作为appium初学者,估计很多人都会遇到这个错误.如何解决: 1.环境变量中去掉appium的安装路径,添加Appium\node_modules\.bin这个路径进去. 2.启动cmd 输入app ...
- MATLAB单步调试
我用的是matlab R2012b 1.先设置断点:点击菜单栏中"EDITOR"--"Breakpoints"--"set",出现以下对话框 ...