当前位置:网站首页>Leetcode question brushing: String 07 (repeated substring)
Leetcode question brushing: String 07 (repeated substring)
2022-06-29 13:48:00 【Taotao can't learn English】
459. Repeated substrings
Given a non empty string , Determine whether it can be made up of a substring of it repeatedly . The given string contains only lowercase letters , And not longer than 10000.
Example 1:
Input : “abab”
Output : True
explain : By substring “ab” Repeat twice to form .
Example 2:
Input : “aba”
Output : False
Example 3:
Input : “abcabcabcabc”
Output : True
explain : By substring “abc” Repeat for four times . ( Or substrings “abcabc” Repeat twice to form .)
I didn't do it , See how complicated the problem is , Read the comment area and finally see one you can understand .
It's just violence , Before each interception i Characters , Copy len / i All over , Compare with the original string .
public boolean repeatedSubstringPattern3(String s) {
// String length
int len = s.length();
// Substring , Does not contain itself
if (len < 2) {
return false;
}
// List all possible
for (int i = 0; i <= len / 2; i++) {
// If it's not divisible , It must not be composed of repeated strings
if (len % i != 0) {
continue;
}
String temp = s.substring(0, i);
String ans = "";
for (int j = 0; j < len / i; j++) {
// len/i individual temp Strings are spliced together and compared with the original string
ans += temp;
}
if (s.equals(ans)) {
return true;
}
}
return false;
}

There are also those big guys who submit in one line .
public boolean repeatedSubstringPattern(String s) {
return (s + s).substring(1, s.length() * 2 - 1).indexOf(s) != -1;
}

The hardest thing is this KMP How to write it
public boolean repeatedSubstringPattern(String s) {
if (s.equals("")) {
return false;
}
int len = s.length();
// Add a space to the original string ( sentry ), Subscript from 1 Start , such j from 0 Start , You don't have to initialize
s = " " + s;
char[] chars = s.toCharArray();
int[] next = new int[len + 1];
// structure next Array ,j from 0 Start ( Space ),i from 2 Start
for (int i = 2, j = 0; i <= len; i++) {
// The match didn't work ,j Back to the previous position ,next The value of the array
while (j > 0 && chars[i] != chars[j + 1]) {
j=next[j];
}
if (chars[i] == chars[j + 1]) {
j++;
}
// to update next The value of the array
next[i] = j;
}
if (next[len] > 0 && len % (len - next[len]) == 0) {
return true;
}
return false;
}

边栏推荐
- Don't build the wheel again. It is recommended to use Google guava open source tool class library. It is really powerful!
- Detailed explanation of machine learning out of fold prediction | using out of fold prediction oof to evaluate the generalization performance of models and build integrated models
- B+ tree | MySQL index usage principle
- 六月集训(第29天) —— 分而治之
- 基于51单片机控制的BUCK开关电源Proteus仿真
- 脚本中的node命令不在控制台打印执行日志
- Prometheus 2.28.0 new features
- Getting started with SQLite3
- Redis deletion policy and eviction algorithm
- Prometheus 2.28.0 新特性
猜你喜欢

#yyds干货盘点# 解决剑指offer:在二叉树中找到两个节点的最近公共祖先

Korean AI team plagiarizes the shock academic world! One tutor with 51 students, or plagiarism recidivist

力扣:合并两个有序链表

Exploring the way of automated testing - Preparation
![[graduation season] it's worth it all the way over the past four years -- advice from the senior students](/img/26/d6363603ff57730970497789a05265.png)
[graduation season] it's worth it all the way over the past four years -- advice from the senior students

思科模拟器简单校园网设计,期末作业难度
![[graduation season · advanced technology Er] 10.76 million graduates, the most difficult employment season in history? I can't roll it up again. I lie down again and again. Where is the road?](/img/d5/7e093b898807b96b89bbe74174990b.png)
[graduation season · advanced technology Er] 10.76 million graduates, the most difficult employment season in history? I can't roll it up again. I lie down again and again. Where is the road?

Weserver publishing map service

在线文本过滤小于指定长度工具

iMile 利用 Zadig 多云环境周部署千次,跨云跨地域持续交付全球业务
随机推荐
Three best practices help enterprises improve supply chain security
LeCun用62页论文公布未来十年研究计划:AI自主智能
Horizon development board configuration network segment
Discard Tkinter! Simple configuration to quickly generate cool GUI!
System. Currenttimemillis() and system Nanotime() which is faster? Most people get the wrong answer!
Cvpr2022 | panopticdepth: a unified framework for depth aware panoramic segmentation
Netdata data data persistence configuration
[untitled] error in installation dependency: refusing to install package with name "* * *" under a package
College girls wear cheongsam to defend! Netizen: the tutor said it would be nice if the paper were as beautiful as the cheongsam
成功解决ValueError: Only TF native optimizers are supported in Eager mode
#yyds干货盘点# 解决剑指offer:在二叉树中找到两个节点的最近公共祖先
Tree array application (acwing 24224244)
思科模拟器简单校园网设计,期末作业难度
The former security director of Uber faced fraud allegations and concealed the data leakage event
使用 Gerrit + Zadig 实现主干开发主干发布(含字节飞书实践)
Hundreds of CVPR people were recruited as new champions. Emoji became a "court witness". M2 MBP was exposed that the hard disk speed was reduced. Today, more big news is here
别再重复造轮子了,推荐使用 Google Guava 开源工具类库,真心强大!
运动App如何实现端侧后台保活,让运动记录更完整?
Weserver publishing map service
【无标题】安装依赖报错:Refusing to install package with name “***“ under a package