forked from aishwarydewangan/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsubsequence_pattern_matching.cpp
48 lines (39 loc) · 1.17 KB
/
subsequence_pattern_matching.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
/*
Given a string and a pattern, write a method to count the number of times the pattern appears
in the string as a subsequence.
Example 1:
Input: string: “baxmx”, patten: "ax"
Output: 2
Explanation: {baxmx, baxmx}.
Example 2:
Input: string: “tomorrow”, pattern: "tor"
Output: 4
Explanation: Following are the four occurences: {tomorrow, tomorrow, tomorrow, tomorrow}.
*/
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> memo;
int patternCount(string& input, string& pattern, int i, int p) {
if(p == pattern.size()) {
return 1;
}
if(i == input.size()) {
return 0;
}
if(memo[i][p] != -1) {
return memo[i][p];
}
int count1 = 0;
if(input[i] == pattern[p]) {
count1 = patternCount(input, pattern, i+1, p+1);
}
int count2 = patternCount(input, pattern, i+1, p);
return (memo[i][p] = count1+count2);
}
int main() {
string input = "baxmx", pattern = "ax";
int n = input.size(), p = pattern.size();
memo.resize(n, vector<int>(p, -1));
cout << "Number of times the pattern appears in the string: " << patternCount(input, pattern, 0, 0);
return 0;
}