当前位置:网站首页>String - 459. Repeated substrings
String - 459. Repeated substrings
2022-07-24 13:54:00 【Xiao Zhao, who is working hard for millions of annual salary】
1 Title Description
Given a non empty string s , Check whether it can be formed by repeating one of its substrings multiple times .
2 Title Example
Example 1:
Input : s = “abab”
Output : true
explain : But by substring “ab” Repeat twice to form .
Example 2:
Input : s = “aba”
Output : false
Example 3:
Input : s = “abcabcabcabc”
Output : true
explain : But by substring “abc” Repeat for four times . ( Or substring “abcabc” Repeat twice to form .)
3 Topic tips
1 <= s.length <= 104
s It's made up of lowercase letters
4 Ideas
Method 1 : string matching
We can put the string ss It's written in s’s’···s’s’ In the form of .
If we remove the string s Before n’ Characters ( That is, a complete s’), Then add these characters to the end of the remaining string in order , Then the resulting string is still s. because 1 ≤ n’≤ n, So if you put two s together , And remove the first and last characters , Then the obtained string — Must include s, namely s It's a substring of it .
So we can consider this method : We will have two s together , And remove the first and last characters . If s Is a substring of the string , that s Meet the requirements of the topic .
Proving requires some tricks of congruence operation , See after method 3 「 Proof of correctness 」 part . Let's assume that we have completed the proof , In this way, you can use very short code to complete this problem . In the following code , We can from the location 11 Start searching , And hope that the query result is not location nn, This is equivalent to removing the first and last characters of the string .
Complexity analysis
Because we use the string lookup function of the language , Therefore, there is no in-depth analysis of its space-time complexity .
Method 2 ::KMP Algorithm
Because this question is to query whether another string appears in one string , You can apply it directly KMP Algorithm . So here's right KMP The algorithm itself will not be repeated . Readers can consult materials for learning .
5 My answer
class Solution {
public boolean repeatedSubstringPattern(String s) {
return (s + s).indexOf(s, 1) != s.length();
}
}
class Solution {
public boolean repeatedSubstringPattern(String s) {
return kmp(s + s, s);
}
public boolean kmp(String query, String pattern) {
int n = query.length();
int m = pattern.length();
int[] fail = new int[m];
Arrays.fill(fail, -1);
for (int i = 1; i < m; ++i) {
int j = fail[i - 1];
while (j != -1 && pattern.charAt(j + 1) != pattern.charAt(i)) {
j = fail[j];
}
if (pattern.charAt(j + 1) == pattern.charAt(i)) {
fail[i] = j + 1;
}
}
int match = -1;
for (int i = 1; i < n - 1; ++i) {
while (match != -1 && pattern.charAt(match + 1) != query.charAt(i)) {
match = fail[match];
}
if (pattern.charAt(match + 1) == query.charAt(i)) {
++match;
if (match == m - 1) {
return true;
}
}
}
return false;
}
}
边栏推荐
- CSDN garbage has no bottom line!
- Apache2 ha experiment with raspberry pie
- 网络安全——文件上传内容检查绕过
- Data modification and insertion
- R语言使用sort函数排序向量数据实战、返回实际排序后的数据(默认升序)
- Statistical table of competition time and host school information of 2022 national vocational college skills competition (the second batch)
- 通配符(泛域名)SSL证书
- Flinktable & SQL (VII)
- 2021-07-09
- Some simple commands
猜你喜欢

Nmap security testing tool tutorial

Hcip day 13

Soft link, hard link

Network security - file upload blacklist bypass

2022.7.22 模拟赛
![[untitled]](/img/67/793d1fd7c295f0af9f683ffa389757.png)
[untitled]

Network security - use exchange SSRF vulnerabilities in combination with NTLM trunking for penetration testing

Csp2021 T3 palindrome

天然气潮流计算matlab程序

Network security - function bypass injection
随机推荐
JS execution mechanism
网络安全——Cookie注入
Csp2021 T3 palindrome
rhcsa第六次笔记
Build ZABBIX monitoring service in LNMP architecture
Network security - Web information collection
Flink comprehensive case (IX)
2022.7.22 simulation match
How to quickly wrap lines in Excel table
Network security - file upload whitelist bypass
【无标题】rhcsa第一次作业
Data modification and insertion
Flink综合案例(九)
Cocoapod installation problems
网络安全——使用Exchange SSRF 漏洞结合NTLM中继进行渗透测试
Game thinking 04 summary: a summary of frame, state and physical synchronization (it was too long before, and now it's brief)
OWASP zap security testing tool tutorial (Advanced)
R语言tidyr包的gather函数将从宽表转化为长表(宽表转化为长表)、第一个参数指定原多个数据列名称生成的新数据列名称、第二个参数指定原表内容值、第三个和第四个参数通过列索引指定不变的列名称列表
网络安全——服务漏洞扫描与利用
Difference between code signing certificate and SSL certificate