当前位置:网站首页>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;
}
}
边栏推荐
- Soft link, hard link
- 2021-07-09
- 代码签名证书与SSL证书区别
- Browser failed to get cookies, browser solution
- 在LNMP架构中搭建Zabbix监控服务
- Game thinking 04 summary: a summary of frame, state and physical synchronization (it was too long before, and now it's brief)
- Network security - use exchange SSRF vulnerabilities in combination with NTLM trunking for penetration testing
- Matlab program for natural gas flow calculation
- OWASP zap security testing tool tutorial (Advanced)
- Introduction to the separation of front and rear platforms of predecessors
猜你喜欢

Network security - penetration using evil maid physical access security vulnerabilities

position: -webkit-sticky; /* for Safari */ position: sticky;

【无标题】

How to quickly wrap lines in Excel table

5年接触近百位老板,身为猎头的我,发现升职的秘密不过4个字

Unity pedestrians walk randomly without collision

Solve the problem of repeated clicking of button uibutton

网络安全——Cookie注入

Aike AI frontier promotion (7.24)

Network security - Cookie injection
随机推荐
Uni app background audio will not be played after the screen is turned off or returned to the desktop
Difference between code signing certificate and SSL certificate
申请了SSL数字证书如何进行域名验证?
Rhcsa sixth note
Nmap安全测试工具使用教程
JS execution mechanism
OWASP zap security testing tool tutorial (Advanced)
RHCE first operation
Soft link, hard link
The KAP function of epidisplay package in R language calculates the value of kappa statistics (total consistency, expected consistency), analyzes the consistency of the results of multiple scoring obj
Lazy loading of pictures
Error importing header file to PCH
Wechat applet todo case
2022年全国职业院校技能大赛赛项比赛时间、承办校信息统计表(第二批)
数据湖系列文章
Network security - Web information collection
2022.7.22 模拟赛
Detailed explanation of switch link aggregation [Huawei ENSP]
使用Activiti创建数据库表报错,
数据修改插入