当前位置:网站首页>Leetcode-- string
Leetcode-- string
2022-06-24 09:20:00 【Rain curtain】
KMP Algorithm : Solve string matching problems
Prefix : A substring containing the first letter but not the last letter
Text string :aabaabaaf
Pattern string :aabaaf
Find the Longest Prefix suffix :
a 0 aa 1 aab 0 aaba 1 aabaa 2 aabaaf 0
The prefix table is 010120
next Array ( In case of conflict, you should go back ):-1 0 -1 0 1 -1
next[0] = 0 Represents a string with only one character , The maximum length of the same pre suffix is 0
344 Reverse string
void reverseString(vector<char>& s) {
for(int i = 0, j = s.size()-1; i < s.size()/2; i++,j--){
swap(s[i],s[j]);
}
}541 every other 2k Before character inversion k Characters
string reverseStr(string s, int k) {
for(int i = 0; i < s.size();i = i + (2 * k)){
//1. every other 2k Characters before k Character inversion
//2. The remaining characters are greater than k Less than 2k Before the k A reversal
if(i + k <= s.size()){
reverse(s.begin()+i,s.begin()+i+k);
continue;
}
//3. The remaining characters are less than k individual , Invert all remaining characters
reverse(s.begin()+i,s.end());
}
return s;
}58 Left rotation string
string reverseLeftWords(string s, int n) {
/*
Local inversion + Global inversion Achieve the purpose of left rotation
1. Reverse the interval [0,n-1]
2. Reverse the interval [n,end]
3. Invert the entire string
*/
reverse(s.begin(),s.begin() + n);
reverse(s.begin()+n,s.end());
reverse(s.begin(),s.end());
return s;
}5 Replace blank space
string replaceSpace(string s){
// Count the number of spaces
int count = 0;
int sOldSize = s.size();
for(int i = 0; i < sOldSize;i++){
if(s[i] == ' ')
count++;
}
// Extended string s Size
s.resize(sOldSize + count * 2);
int sNewSize = s.size();
// Replace spaces from back to front
for(int i = sNewSize - 1,j = sOldSize - 1 ; j < i ; i--,j--){
if(s[j] != ' '){
s[i] = s[j];
}else{
s[i] = '0';
s[i-1] = '2';
s[i-2] = '%';
i -= 2;
}
}
return s;
}151 Invert the words in the string
original : abc_def After reversing : def_abc
First reverse each word cba_fed ; Inverting the entire string def_abc
28 Realization strStr
kmp Algorithm
void getNext(int* next,string& s){
int j = 0;//j Is the end of the prefix
next[0] = 0;
for(int i = 1; i < s.size(); i++){ //i Is the end of the suffix
while(j > 0 && s[i] != s[j]){
j = next[j - 1];
}
if(s[i] == s[j]){
j++;
}
next[i] = j;
}
}
int strStr(string haystack, string needle) {
if(needle.size() == 0) return 0;
int next[needle.size()];
getNext(next,needle); //next The values in the array represent the prefix such as longest appearance
int j = 0;
for(int i = 0; i < haystack.size(); i++){
while(j > 0 && haystack[i] != needle[j]){
j = next[j - 1];
}
if(haystack[i] == needle[j]){
j++;
}
if(j == needle.size()){
return (i - needle.size() + 1); // 3 - 2 + 1
}
}
return -1;
}459 Repeated substrings
void getNext (int* next, const string& s){
next[0] = 0;
int j = 0;
for(int i = 1;i < s.size(); i++){
while(j > 0 && s[i] != s[j]) {
j = next[j - 1];
}
if(s[i] == s[j]) {
j++;
}
next[i] = j;
}
}
bool repeatedSubstringPattern (string s) {
if (s.size() == 0) {
return false;
}
int next[s.size()];
getNext(next, s);
int len = s.size();
if (next[len - 1] != 0 && len % (len - (next[len - 1] )) == 0) {
return true;
}
return false;
}边栏推荐
- Numpy numpy中的np.c_和np.r_详解
- Every (), map (), forearch () methods. There are objects in the array
- MYCAT read / write separation and MySQL master-slave synchronization
- Cmake命令之target_compile_options
- P6117-[JOI 2019 Final]コイン集め【贪心】
- Easyexcel single sheet and multi sheet writing
- Leetcode -- wrong set
- Essay - Reflection
- [noi Simulation Competition] send (tree DP)
- Data middle office: middle office practice and summary
猜你喜欢

Data middle office: middle office architecture and overview

零基础自学SQL课程 | 子查询

Time Series Data Augmentation for Deep Learning: A Survey 之论文阅读

Squid代理服务器应用

EasyExcel单sheet页与多sheet页写出

Numpy NP in numpy c_ And np r_ Explain in detail

4275. Dijkstra sequence

YOLOX backbone——CSPDarknet的实现
![[noi Simulation Competition] geiguo and time chicken (structure)](/img/4c/ed1b5bc2bed653c49b8b7922ce1674.png)
[noi Simulation Competition] geiguo and time chicken (structure)

4274. suffix expression
随机推荐
[quantitative investment] discrete Fourier transform to calculate array period
Common emoticons
荐书丨《好奇心的秘密》:一个针尖上可以站多少跳舞的小天使?
cookie加密 4 rpc方法确定cookie加密
Huawei Router: IPSec Technology
【LeetCode】387. First unique character in string
Target detection series fast r-cnn
Zero foundation self-study SQL course | syntax sequence and execution sequence of SQL statements
软件系统依赖关系分析
Qingcloud based "real estate integration" cloud solution
零基础自学SQL课程 | 子查询
普通人没有学历,自学编程可以月入过万吗?
Weekly recommended short video: talk about "meta universe" with a serious attitude
可直接套用的Go编码规范
华为路由器:GRE技术
【LeetCode】415. String addition
2021-05-20computed and watch applications and differences
Ebanb B1 Bracelet brush firmware abnormal interrupt handling
The border problem after the focus of input
听说你还在花钱从网上买 PPT 模板?