-
Notifications
You must be signed in to change notification settings - Fork 0
/
sub_sequence.cpp
52 lines (38 loc) · 1.25 KB
/
sub_sequence.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include<iostream>
#include<cstdlib>
#include<vector>
using namespace std;
bool isSubsequence(vector<string> s1, vector<string> s2){
int j = 0;
for(int i=0; i<s2.size() && j<s1.size(); i++){ //Traverse s2 and s1
if(s1[j].compare(s2[i]) == 0) //Compare current event of s2 with first unmatched char of s1
j++; //if matched then move ahead in s1 (matching is case sensitive)
}
return ( j==s1.size() ); // Return true, if all events of s1 were found in s2 in same order
}
int main(){
vector<string> s1, s2;
cout<<"\n\nEnter Sequence 1\n ";
while(true){
string mid_string;
cout<<"\nEnter next String : ";
cin>>mid_string;
if(mid_string.compare("-1") == 0) //to stop taking input in sequence 1
break;
s1.push_back(mid_string);
}
cout<<"\n\nEnter Sequence 2\n ";
while(true){
string mid_string;
cout<<"\nEnter next String: ";
cin>>mid_string;
if(mid_string.compare("-1") == 0) //to stop taking input in sequence 2
break;
s2.push_back(mid_string);
}
if( isSubsequence(s1, s2)) // isSubsequence returns 'TRUE' if s1 is subsequence of s2.
cout<<"\n\nYes, sequence 1 is a subsequence of sequence 2.";
else
cout<<"\n\nNo, sequence 1 is not a subsequence of sequence 2.";
return 0;
}