-
Notifications
You must be signed in to change notification settings - Fork 29
/
WildCardMatching.java
144 lines (136 loc) · 3.9 KB
/
WildCardMatching.java
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package DP;
public class WildCardMatching {
public static void main(String[] args) {
String src = "abcdef";
String pat = "a?*********";
System.out.println(WildCardMatchingRecursin(src, pat));
System.out.println(WildCardMatchingRecursin(src, pat, 0, 0));
System.out.println(WildCardMatchingTD(src, pat, 0, 0, new int[src.length()][pat.length()]));
System.out.println(WildCardMatchingBU(src, pat));
}
public static boolean WildCardMatchingRecursin(String src, String pat) {
if (src.length() == 0 && pat.length() == 0) {
return true;
}
if (src.length() != 0 && pat.length() == 0) {
return false;
}
if (src.length() == 0 && pat.length() != 0) {
for (int i = 0; i < pat.length(); i++) {
if (pat.charAt(i) != '*') {
return false;
}
}
return true;
}
boolean ans;
char chs = src.charAt(0);
char chp = pat.charAt(0);
String ros = src.substring(1);
String rop = pat.substring(1);
if (chs == chp || chp == '?') {
ans = WildCardMatchingRecursin(ros, rop);
} else if (chp == '*') {
boolean blank = WildCardMatchingRecursin(src, rop);
boolean multiple = WildCardMatchingRecursin(ros, pat);
ans = blank || multiple;
} else {
ans = false;
}
return ans;
}
public static boolean WildCardMatchingRecursin(String src, String pat, int svidx, int pvidx) {
if (src.length() == svidx && pat.length() == pvidx) {
return true;
}
if (src.length() != svidx && pat.length() == pvidx) {
return false;
}
if (src.length() == svidx && pat.length() != pvidx) {
for (int i = pvidx; i < pat.length(); i++) {
if (pat.charAt(i) != '*') {
return false;
}
}
return true;
}
boolean ans;
char chs = src.charAt(svidx);
char chp = pat.charAt(pvidx);
if (chs == chp || chp == '?') {
ans = WildCardMatchingRecursin(src, pat, svidx + 1, pvidx + 1);
} else if (chp == '*') {
boolean blank = WildCardMatchingRecursin(src, pat, svidx, pvidx + 1);
boolean multiple = WildCardMatchingRecursin(src, pat, svidx + 1, pvidx);
ans = blank || multiple;
} else {
ans = false;
}
return ans;
}
// TD
public static boolean WildCardMatchingTD(String src, String pat, int svidx, int pvidx, int[][] strg) {
if (src.length() == svidx && pat.length() == pvidx) {
return true;
}
if (src.length() != svidx && pat.length() == pvidx) {
return false;
}
if (src.length() == svidx && pat.length() != pvidx) {
for (int i = pvidx; i < pat.length(); i++) {
if (pat.charAt(i) != '*') {
return false;
}
}
return true;
}
if (strg[svidx][pvidx] != 0) {
return strg[svidx][pvidx] == 1 ? true : false;
}
boolean ans;
char chs = src.charAt(svidx);
char chp = pat.charAt(pvidx);
if (chs == chp || chp == '?') {
ans = WildCardMatchingTD(src, pat, svidx + 1, pvidx + 1, strg);
} else if (chp == '*') {
boolean blank = WildCardMatchingTD(src, pat, svidx, pvidx + 1, strg);
boolean multiple = WildCardMatchingTD(src, pat, svidx + 1, pvidx, strg);
ans = blank || multiple;
} else {
ans = false;
}
strg[svidx][pvidx] = ans == true ? 1 : 2;
return ans;
}
// BU
public static boolean WildCardMatchingBU(String src, String pat) {
boolean[][] strg = new boolean[src.length() + 1][pat.length() + 1];
strg[src.length()][pat.length()] = true;
for (int row = src.length(); row >= 0; row--) {
for (int col = pat.length() - 1; col >= 0; col--) {
if (row == src.length()) {
if (pat.charAt(col) == '*') {
strg[row][col] = strg[row][col + 1];
} else {
strg[row][col] = false;
}
} else {
boolean ans;
char chs = src.charAt(row);
char chp = pat.charAt(col);
if (chs == chp || chp == '?') {
ans = strg[row + 1][col + 1];
} else if (chp == '*') {
boolean blank = strg[row + 1][col];
boolean multiple = strg[row][col + 1];
ans = blank || multiple;
} else {
ans = false;
}
strg[row][col] = ans;
}
}
}
return strg[0][0];
}
}