当前位置:网站首页>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;
}
}
边栏推荐
- Flink综合案例(九)
- Some simple commands
- How to install PHP 5.6 on Ubuntu 18.04 and Debian 9
- XSS white list
- R language uses the sum function of epidisplay package to calculate the descriptive statistical summary information of the specified variables in dataframe under different grouping variables, visualiz
- 关于不定方程解的个数的问题
- 三层交换机配置MSTP协议详解【华为eNSP实验】
- Flink高级特性和新特性(八)
- Network security - file upload whitelist bypass
- 天然气潮流计算matlab程序
猜你喜欢

Why are there "two abstract methods" in the functional interface comparator?

Network security - penetration using evil maid physical access security vulnerabilities

rhce第一次作业

Apache2 ha experiment with raspberry pie

网络安全——文件上传竞争条件绕过

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

Nmap安全测试工具使用教程

uni-app 背景音频 熄屏或者退回桌面之后不在播放

RHCE first operation

Unity行人随机行走不碰撞
随机推荐
The latest and complete Flink series tutorials in 2021_ Preliminary exploration of Flink principle and flow batch integration API (II. V) V2
On the number of solutions of indefinite equations
R language uses the statstack function of epidisplay package to view the statistics (mean, median, etc.) of continuous variables and the corresponding hypothesis test in a hierarchical manner based on
2022.7.22 模拟赛
How to quickly wrap lines in Excel table
Network security - filtering bypass injection
一些简单命令
微信小程序 TODO案例
R语言检验样本比例:使用prop.test函数执行单样本比例检验计算总体中成功样本比例p值的置信区间
2021年最新最全Flink系列教程_Flink原理初探和流批一体API(二.五)v2
rhcsa第六次笔记
Solve the problem of repeated clicking of button uibutton
R语言使用epiDisplay包的tableStack函数制作统计汇总表格(基于目标变量分组的描述性统计、假设检验等)、设置by参数为目标变量、设置percent参数配置是否显示百分比信息
Browser type judgment
Network security - Web information collection
关于不定方程解的个数的问题
Data type binary string type
Unity pedestrians walk randomly without collision
栈与队列——232. 用栈实现队列
[untitled] rhcsa first operation