LeetCode 459. 重复的子字符串(Repeated Substring Pattern)
459. 重复的子字符串
459. Repeated Substring Pattern
题目描述
给定一个非空的字符串,判断它是否可以由它的一个子串重复多次构成。给定的字符串只含有小写英文字母,并且长度不超过 10000。
LeetCode459. Repeated Substring Pattern
示例 1:
输出: True
解释: 可由子字符串 "ab" 重复两次构成。
示例 2:
输出: False
示例 3:
输出: True
解释: 可由子字符串 "abc" 重复四次构成。(或者子字符串 "abcabc" 重复两次构成。)
Java 实现
class Solution {
public boolean repeatedSubstringPattern1(String s) {
int n = s.length();
for (int i = n / 2; i > 0; i--) {
if (n % i == 0) {
boolean flag = true;
for (int j = n / i; j > 1; j--) {
if (!s.substring(0, i).equals(s.substring(i * (j - 1), i * j))) {
flag = false;
break;
}
}
if (flag) {
return true;
}
}
}
return false;
}
public boolean repeatedSubstringPattern2(String s) {
String str = s + s;
return str.substring(1, str.length() - 1).contains(s);
}
}
相似题目
参考资料
- https://leetcode.com/problems/repeated-substring-pattern/
- https://leetcode.com/problems/repeated-substring-pattern/
LeetCode 459. 重复的子字符串(Repeated Substring Pattern)的更多相关文章
- [Swift]LeetCode459. 重复的子字符串 | Repeated Substring Pattern
Given a non-empty string check if it can be constructed by taking a substring of it and appending mu ...
- Java实现 LeetCode 459 重复的子字符串
459. 重复的子字符串 给定一个非空的字符串,判断它是否可以由它的一个子串重复多次构成.给定的字符串只含有小写英文字母,并且长度不超过10000. 示例 1: 输入: "abab" ...
- Leetcode 459.重复的子字符串
重复的子字符串 给定一个非空的字符串,判断它是否可以由它的一个子串重复多次构成.给定的字符串只含有小写英文字母,并且长度不超过10000. 示例 1: 输入: "abab" 输出: ...
- LeetCode 686. 重复叠加字符串匹配(Repeated String Match)
686. 重复叠加字符串匹配 686. Repeated String Match 题目描述 给定两个字符串 A 和 B,寻找重复叠加字符串 A 的最小次数,使得字符串 B 成为叠加后的字符串 A 的 ...
- [LeetCode] Repeated Substring Pattern 重复子字符串模式
Given a non-empty string check if it can be constructed by taking a substring of it and appending mu ...
- 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 ...
- LeetCode_459. Repeated Substring Pattern
459. Repeated Substring Pattern Easy Given a non-empty string check if it can be constructed by taki ...
- 459 Repeated Substring Pattern 重复的子字符串
给定一个非空的字符串,判断它是否可以由它的一个子串重复多次构成.给定的字符串只含有小写英文字母,并且长度不超过10000.示例 1:输入: "abab"输出: True解释: 可由 ...
随机推荐
- 《挑战30天C++入门极限》C/C++中字符串常量的不相等性及字符串的Copy
C/C++中字符串常量的不相等性及字符串的Copy #include <iostream> void main(void) { if("test&quo ...
- word 插入表格,位置不在最左边
首先想到的是 样式的问题.
- Pytest权威教程14-缓存:使用跨执行状态
目录 缓存:使用跨执行状态 使用方法 首先只重新运行故障或故障 上次运行中没有测试失败时的行为 新的config.cache对象 检查缓存内容 清除缓存内容 逐步修复失败用例 unittest.Tes ...
- Python 程序打包成 exe 可执行文件
Python 程序打包工具 Python 是一个脚本语言,被解释器解释执行.它的发布方式: .py 文件:对于开源项目或者源码没那么重要的,直接提供源码,需要使用者自行安装 Python 并且安装依赖 ...
- SpringBoot整合ElasticSearch:基于SpringDataElasticSearch
0.注意事项 SpringDataElasticSearch可能和远程的ElasticSearch版本不匹配,会宝座 版本适配说明:https://github.com/spring-projects ...
- 讨论关于RAID以及RAID对于存储的影响
定义及作用 RAID是Redundent Array of Inexpensive Disks的缩写,直译为“廉价冗余磁盘阵列”,也简称为“磁盘阵列”.后来RAID中的字母I被改作了Independe ...
- Java 面向对象(十七)
第一章 File类 1.1 概述 java.io.File 类是文件和目录路径名的抽象表示,主要用于文件和目录的创建.查找和删除等操作. 1.2 构造方法 public File(String pat ...
- The difference between a bad programmer and a good one is whether he considers his code or his data structures more important. Bad programmers worry about the code. Good programmers worry about data
重构:改善饿了么交易系统的设计思路 原创: 盛赫 阿里巴巴中间件 昨天
- lintcode 787. The Maze 、788. The Maze II 、
787. The Maze https://www.cnblogs.com/grandyang/p/6381458.html 与number of island不一样,递归的函数返回值是bool,不是 ...
- Mysql的三种数据类型
Mysql的三种数据类型 1.数值类型 2.日期和时间类型 3.字符串类型 00x1 [数值类型] 00x2 [日期和时间类型] 00x3 [字符串类型]