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 ...
随机推荐
- mysql5.7.20更改root密码
my.cnf 中在[mysqld]下面增加 skip-grant-tables 使用空密码登录数据库执行下面命令 update mysql.user set authentication_string ...
- 第二章 向量(d2)有序向量:二分查找
- struct和union,enum分析
空结构体占用的内存多大? struct d { }; int main() { struct d d1; struct d d2; printf("%d,%0x\n",sizeof ...
- RxJS之工具操作符 ( Angular环境 )
一 delay操作符 源Observable延迟指定时间,再开始发射值. import { Component, OnInit } from '@angular/core'; import { of ...
- TZOJ 4325 RMQ with Shifts(线段树查询最小,暴力更新)
描述 In the traditional RMQ (Range Minimum Query) problem, we have a static array A. Then for each que ...
- Redis 与Spring-data-redis 整合后封装的工具类
此工具类方法是使用了redis 与spring 整合后,才可以使用的工具类,将 spring-data-redis 一些我们开发中常用的方法进行了封装,方便我们日常开发中进行调用: package c ...
- linux服务器搭建
centos7 java web项目环境搭配 2018年07月19日 17:20:21 阅读数:25 首先进行系统安装,此处不进行详细介绍,自行百度安装 一.配置ip地址信息 1.进入/etc/sys ...
- c代码片段-注解
#include<stdio.h> /* * int ac 是命令行参数的个数 第一个参数是当前文件地址 * char * arg[] 字符指针的数组, 每一个指针指向一个具体的命令行参数 ...
- vs2017连接mysql以及问题汇总
https://www.cnblogs.com/eye-like/p/8494355.html https://blog.csdn.net/u012658972/article/details/791 ...
- Hadoop集群配置(最全面总结 )(转)
Hadoop集群配置(最全面总结) huangguisu 通常,集群里的一台机器被指定为 NameNode,另一台不同的机器被指定为JobTracker.这些机器是masters.余下的机器即作为Da ...