chore(CPlusPlus): add sliding windows (#853)
parent
fe130ddc4f
commit
c6a454590d
|
@ -33,6 +33,7 @@
|
|||
- [Sparse Matrix](Arrays/sparse_matrix.cpp)
|
||||
|
||||
|
||||
|
||||
## Dynamic-Programming
|
||||
|
||||
- [Longest Common Subsequence](Dynamic-Programming/longest-common-subsequence.cpp)
|
||||
|
@ -128,6 +129,7 @@
|
|||
- [Boyer Moore pattern search](Strings/Boyer_Moore.cpp)
|
||||
- [Longest common prefix](Strings/longest-common-prefix.cpp)
|
||||
- [First unique character in the string](Strings/first-unique-character.cpp)
|
||||
- [Sliding Window to match target string](Strings/sliding-window.cpp)
|
||||
|
||||
## Trees
|
||||
|
||||
|
|
|
@ -0,0 +1,42 @@
|
|||
/*
|
||||
Description: A program to find target sub string in given string
|
||||
|
||||
Approach: Using sliding window technique to compare every possible substring with the target string.
|
||||
It also supports variable length target inputs since we are initialising window size with size of target
|
||||
|
||||
Time complexity: O(n)
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
void sliding(string s,string target){
|
||||
int window_size=target.size();
|
||||
bool notfound=true;
|
||||
for(int i=0;i<s.size();i++){
|
||||
|
||||
string value = s.substr(i,window_size);
|
||||
|
||||
if(target==value){
|
||||
cout<<target<<" found at "<<i<<" and "<<i+window_size<<endl;
|
||||
notfound = false;
|
||||
}
|
||||
}
|
||||
if(notfound)
|
||||
cout<<"Target Not found";
|
||||
}
|
||||
|
||||
int main() {
|
||||
string target="Ipsum";
|
||||
string s = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsu";
|
||||
sliding(s,target);
|
||||
|
||||
cout<<"\nenter the target string:";
|
||||
cin>>target;
|
||||
|
||||
sliding(s,target);
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue