-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathmax_possible_subsequence.cpp
68 lines (58 loc) · 1.95 KB
/
max_possible_subsequence.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
Given two words and a string.
By using two words , Count How many different subsequence's can be created through the string.
Two subsequence's are considered to be different if they start or end
at different positions in the string.
*/
#include <bits/stdc++.h>
using namespace std;
//From this get_count_max_possible_subsequence() function we will count max different subsequence's can be created
int get_count_max_possible_subsequence(string word1, string word2, string text)
{
int size_of_text = text.size();
int size_of_word1 = word1.size();
int size_of_word2 = word2.size();
int count_max_possible_subsequence = 0;
int count1 = 0;
for(int i = 0; i < size_of_text; i++)
{
/* here we will check if string s1 is first word or not
if yes we will increment the count1
*/
string s1 = text.substr(i , size_of_word1);
if(s1 == word1)
count1++;
string s2 = text.substr(i , size_of_word2);
if(s2 == word2)
{
/* here we will check if string s2 is second word or not
if yes we will increment the count_max_possible_subsequence
with current count1 value
*/
count_max_possible_subsequence += count1;
}
}
return count_max_possible_subsequence;
}
int main()
{
string text, word1, word2;
cout << "Enter the two words : \n";
cin >> word1 >> word2;
cout << "Enter the string : \n";
cin >> text;
int count_max_possible_subsequence = get_count_max_possible_subsequence(word1, word2, text);
cout << "Maximum possible subsequence can be created is : ";
cout << count_max_possible_subsequence << endl;
}
/*
Standard Input and Output
Enter the two words :
KICK
START
Enter the string :
KICKXKICKXSTARTXKICKXSTART
Maximum possible subsequence can be created is : 5
Time Complexity : O( N )
Space Complexity : O( N )
*/