当前位置:网站首页>leetcode刷题:字符串07(重复的子字符串)
leetcode刷题:字符串07(重复的子字符串)
2022-06-29 10:43:00 【涛涛英语学不进去】
459.重复的子字符串
给定一个非空的字符串,判断它是否可以由它的一个子串重复多次构成。给定的字符串只含有小写英文字母,并且长度不超过10000。
示例 1:
输入: “abab”
输出: True
解释: 可由子字符串 “ab” 重复两次构成。
示例 2:
输入: “aba”
输出: False
示例 3:
输入: “abcabcabcabc”
输出: True
解释: 可由子字符串 “abc” 重复四次构成。 (或者子字符串 “abcabc” 重复两次构成。)
我是没做出来了,看题解好复杂,看评论区终于看到一个自己能看懂的。
就是暴力一个个找,每位截取前 i 个字符,复制 len / i 遍,和原字符串进行比较。
public boolean repeatedSubstringPattern3(String s) {
//字符串长度
int len = s.length();
//子串,不包含自身
if (len < 2) {
return false;
}
//列举所有可能
for (int i = 0; i <= len / 2; i++) {
//如果不能整除,必定不是重复字符串组成
if (len % i != 0) {
continue;
}
String temp = s.substring(0, i);
String ans = "";
for (int j = 0; j < len / i; j++) {
// len/i 个 temp 字符串拼接在一起和原字符串比较
ans += temp;
}
if (s.equals(ans)) {
return true;
}
}
return false;
}

也有那些一行提交的大佬们。
public boolean repeatedSubstringPattern(String s) {
return (s + s).substring(1, s.length() * 2 - 1).indexOf(s) != -1;
}

最难的还是这个KMP写法
public boolean repeatedSubstringPattern(String s) {
if (s.equals("")) {
return false;
}
int len = s.length();
//原串加个空格(哨兵),使下标从1开始,这样j从0开始,也不用初始化了
s = " " + s;
char[] chars = s.toCharArray();
int[] next = new int[len + 1];
//构造next数组,j从0开始(空格),i从2开始
for (int i = 2, j = 0; i <= len; i++) {
//匹配不成功,j 回到前一位置,next数组对应的值
while (j > 0 && chars[i] != chars[j + 1]) {
j=next[j];
}
if (chars[i] == chars[j + 1]) {
j++;
}
//更新next数组的值
next[i] = j;
}
if (next[len] > 0 && len % (len - next[len]) == 0) {
return true;
}
return false;
}

边栏推荐
- Evaluation of IP location query interface Ⅱ
- The use of Fibonacci sequence and bubble sort in C language
- Interview questions of Tencent automation software test of CSDN salary increase secret script (including answers)
- Object 类——万类之父
- 软件工程导论——第五章——总体设计
- Qt学习07 Qt中的坐标系统
- 中科方德技术专家直播:如何基于 OpenStack、Ceph 构建私有云平台? | 第 27 期
- The use of variables in shell that you have to be familiar with
- Haitai Advanced Technology | application of privacy computing technology in medical data protection
- 【每日3题(3)】重新格式化电话号码
猜你喜欢

Data analysis method and Thinking: funnel analysis

Specific method and example program of Siemens s7-200smart control stepping motor

安全 创新 实践|海泰方圆受邀参加“数字时代的网信创新与价值共创”技术交流研讨会

QT learning 16 parent-child relationships between QT objects

Adding sharding sphere5.0.0 sub tables to the ruoyi framework (adding custom sub table policies through SPI)

Object 类——万类之父

Cornerstone of efficient remote office: effective communication | community essay solicitation

【每日3题(1)】判断国际象棋棋盘中一个格子的颜色

影响LED封装散热主要因素有哪些?

Oracle NetSuite 助力 TCM Bio,洞悉数据变化,让业务发展更灵活
随机推荐
云原生开发必备:首个通用无代码开发平台 iVX 编辑器
【高并发】2.8万字的Callable和Future面试知识点总结,看完我直接面进了字节跳动,原谅我有点飘了(上)
Several types of MySQL index invalidation and their solutions
【HBZ分享】AQS + CAS +LockSupport 实现ReentrantLock的原理
Exclusive interview with CTO: the company has deepened the product layout and accelerated the technological innovation of domestic EDA
Multi thread communication between client and server (primary version)
(JS) status mode
信息技术应用创新专业人员(数据库)中级培训火热招生中(7月6-10日)
Oracle扩充分布式云端服务 为更多客户带来全面公有云服务
【毕业季】总结过去,展望未来
Encore une fois, le chemin de l'amélioration de redis Cloud
Pipeline aggregations管道聚合-Sibling-1
稳定币风险状况:USDT 和 USDC 安全吗?
Google Earth engine (GEE) - Gedi L2a vector canopy top height (version 2) global ecosystem data set
Nature | biosynthetic potential of global marine microbiome
Qt学习09 计算器界面代码重构
[daily 3 questions (3)] reformat the phone number
Take another picture of cloud redis' improvement path
高效远程办公的基石:有效沟通 |社区征文
Data analysis method and Thinking: funnel analysis