-
Notifications
You must be signed in to change notification settings - Fork 160
/
Orderedminimumwindowsubstring.cpp
69 lines (55 loc) · 1.55 KB
/
Orderedminimumwindowsubstring.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 a string S and a string T, find the minimum window in S which will
contain all the characters in T in the orders as it appears in T in
complexity O(n).
S = "ADOBECODEBANC"
T = "ABC"
Minimum window is "ADOBEC"
*/
/*
solution:
suppose we find string t in string s. dp[i][j] means that the minimal length of finding the substring in T from index j in s from index i.
so, s[i,i+dp[i][j]-1] is the minimal length window we wants.
O(n*m) time, O(n*m) space
*/
#include<iostream>
#include<string>
using namespace std;
//just for simplicity
int dp[20][20];
void MinWindows(string s, string t) {
int m = s.size();
int n = t.size();
int minindex = -1;
//assume not match at first
memset(dp, -1, sizeof(dp));
//always match empty string
for (int i = 0; i <= m; i++) {
dp[i][n] = 0;
}
for (int i = m-1;i >= 0; i--) {
for (int j = n-1;j >= max(0, n-(m-i)); j--) {
//match and increase length by one
if (s[i] == t[j] && dp[i+1][j+1]>=0) {
dp[i][j] = dp[i+1][j+1]+1;
//check if previous i position matches
} else if (dp[i+1][j] >= 0) {
dp[i][j] = dp[i+1][j]+1;
}
}
//match all chars in t & update minimal length
if (dp[i][0] >= 0 && (minindex == -1 || dp[i][0] < dp[minindex][0])) {
minindex = i;
}
}
if (minindex>=0) {
cout<<"start: "<<minindex<<" ";
cout<<"len: "<<dp[minindex][0]<<endl;
}
}
int main() {
string s("ADOBECODEBANC");
string t("ABC");
MinWindows(s, t);
return 0;
}