当前位置:网站首页>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;
}

边栏推荐
- 斐波那锲数列与冒泡排序法在C语言中的用法
- Qt学习07 Qt中的坐标系统
- Uber前安全主管面临欺诈指控 曾隐瞒数据泄露事件
- Modbustcp protocol WiFi wireless learning single channel infrared module (round shell version)
- How to test the performance of container platform, including stability, expansion efficiency and component performance
- When a technician becomes a CEO, what "bugs" should be modified?
- 信息技术应用创新专业人员(数据库)中级培训火热招生中(7月6-10日)
- What is the experience of working in an IT company in Japan?
- Nature | 全球海洋微生物组的生物合成潜力
- Modbustcp protocol network learning single channel infrared module (double-layer board)
猜你喜欢

斐波那锲数列与冒泡排序法在C语言中的用法

Discussion on QT learning 10 message processing in QT

Evaluation of IP location query interface Ⅱ

QT learning 15 separation of user interface and business logic

Rebuild confidence in China's scientific research - the latest nature index 2022 released that China's research output increased the most

The use of Fibonacci sequence and bubble sort in C language

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

Modbus RTU protocol 485 learning 2-way infrared module

Qt学习01 GUI程序原理分析

The first "cyborg" in the world died, and he only transformed himself to "change his life against the sky"
随机推荐
QT learning 11 string classes in QT
记一次 MSI 笔记本 GE63 播放网页视频 闪屏和随机发绿 问题解决
直击产业落地!飞桨重磅推出业界首个模型选型工具
Specific method and example program of Siemens s7-200smart control stepping motor
Shell quotation marks and escape are rarely noticed, but they are often used in writing scripts
Object 类——万类之父
The former security director of Uber faced fraud allegations and concealed the data leakage event
面试高并发,凉了!!(全程高能,建议收藏)
Pipeline aggregations管道聚合-Sibling-1
Modbustcp protocol network learning single channel infrared module (double-layer board)
X-FRAME-OPTIONS web page hijacking vulnerability
【每日3题(3)】重新格式化电话号码
Take another picture of cloud redis' improvement path
Uber前安全主管面临欺诈指控 曾隐瞒数据泄露事件
Course design for the end of the semester: product sales management system based on SSM
Nature | biosynthetic potential of global marine microbiome
Good news | Haitai Fangyuan has passed the cmmi-3 qualification certification, and its R & D capability has been internationally recognized
重建中国科研自信——2022最新自然指数排行榜(Nature Index 2022 )公布,中国的研究产出增幅最大...
How to properly manage temporary files when writing shell scripts
【HBZ分享】AQS + CAS +LockSupport 实现ReentrantLock的原理