【LeetCode】459. Repeated Substring Pattern 解题报告(Java & Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
[LeetCode]
题目地址:https://leetcode.com/problems/repeated-substring-pattern/
- Difficulty: Easy
题目描述
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.)
题目大意
判断一个字符串能不能由比它更小的子串构成。
解题方法
遍历子串
思路是这样的:
- 重复的子串的长度肯定是总长度的因子。
- 遍历所有的因子,从length/2开始
- 如果i是length的因子,把从0到i这个substring重复length()/i倍
- 看看这个重复后的串是否与原来的串相等。
public class Solution {
public boolean repeatedSubstringPattern(String str) {
int l = str.length();
for(int i=l/2;i>=1;i--) {
if(l%i==0) {
int m = l/i;
String subS = str.substring(0,i);
StringBuilder sb = new StringBuilder();
for(int j=0;j<m;j++) {
sb.append(subS);
}
if(sb.toString().equals(str)) return true;
}
}
return false;
}
}
AC: 48 ms
二刷, python.
现在再刷这个题的时候因为用的Python,已经对字符串处理了然于胸了,所以很快就写出了下面的答案:
同样是遍历因子,然后把新的切片乘以倍数,看得到的字符串是否和源字符串相同。
class Solution:
def repeatedSubstringPattern(self, s):
"""
:type s: str
:rtype: bool
"""
len_s = len(s)
for i in range(1, len_s // 2 + 1):
if len_s % i == 0:
sub_s = s[:i]
if sub_s * (len_s // i) == s:
return True
return False
日期
2017 年 1 月 15 日
2018 年 7 月 4 日
2018 年 11 月 22 日 —— 感恩节快乐~
【LeetCode】459. Repeated Substring Pattern 解题报告(Java & Python)的更多相关文章
- 【LeetCode】456. 132 Pattern 解题报告(Python)
[LeetCode]456. 132 Pattern 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...
- 43. leetcode 459. Repeated Substring Pattern
459. Repeated Substring Pattern Given a non-empty string check if it can be constructed by taking a ...
- [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】383. Ransom Note 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...
- 【LeetCode】575. Distribute Candies 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- 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 ...
- 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 ...
- KMP - LeetCode #459 Repeated Substring Pattern
复习一下KMP算法 KMP的主要思想是利用字符串自身的前缀后缀的对称性,来构建next数组,从而实现用接近O(N)的时间复杂度完成字符串的匹配 对于一个字符串str,next[j] = k 表示满足s ...
- 【LeetCode】136. Single Number 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 异或 字典 日期 [LeetCode] 题目地址:h ...
随机推荐
- Python基础笔记1
这篇笔记来自廖雪峰的Python教程. 一.Python基础 Python使用缩进来组织代码块,务必遵守约定俗成的习惯,坚持使用4个空格的缩进. 在文本编辑器中,需要设置把Tab自动转换为4个空格,确 ...
- Python基础之流程控制for循环
目录 1. 语法 2. for+break 3. for+continue 4. for循环嵌套 1. 语法 while循环可以对任何内容循环,但循环次数不可控 for循环基于容器类型的长度,循环次数 ...
- 移动测试(web和app)及app测试实战
移动测试androidiosapp上 原生GUI 混合应用H5 web端兼容性浏览器测试需要的内容:safari 浏览器edge浏览器ie11浏览器firefox浏览器chrome浏览器 国内360浏 ...
- 【.Net】使用委托实现被引用的项目向上级项目的消息传递事件
前言:在实际项目过程中,经常可能遇到被引用的项目要向上传递消息,但是又不能通过方法进行返回等操作,这个时候委托就派上用场了.以下使用委托,来实现被引用的项目向上传递消息的小教程,欢迎各位大佬提供建议. ...
- Qt5的安装和编译
Ubuntu18.04安装Qt5 1.配置unbuntu 和宿主机共享文件夹安装vmware-tools 2.下载 Qt http://download.qt.io/archive/qt/ 3.修改 ...
- Spring中的InitializingBean与DisposableBean
InitializingBean顾名思义,应该是初始化Bean相关的接口. 先看一下该接口都定义了哪些方法: public interface InitializingBean { void afte ...
- Oracle学习笔记(1)
折腾了好久 终于把oracle安装成功了.小兴奋下. 创建了一个数据库 dabook. run--> Services.msc查看服务: 可以看到DABOOK的服务已启动. 1,sys用户 在c ...
- python的urllib学习
1.基本方法 urllib.request.urlopen(url, data=None, [timeout, ]*, cafile=None, capath=None, cadefault=Fals ...
- 莫烦python教程学习笔记——使用波士顿数据集、生成用于回归的数据集
# View more python learning tutorial on my Youtube and Youku channel!!! # Youtube video tutorial: ht ...
- Java 将Word转为OFD
通常在工作中比较常用到的Microsoft Word是属于国外的文档内容编辑软件,其编译技术均属国外.而OFD是一种我国的自主文档格式,在某些特定行业或企业的文档存储技术上是一种更为安全的选择.下面将 ...