Skip to content

Commit adc33c0

Browse files
authored
Create WildCardMatching.java
1 parent 839697f commit adc33c0

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
https://github.com/mission-peace/interview/blob/master/src/com/interview/dynamic/WildCardMatching.java
3+
4+
Implement wildcard pattern matching with support for '?' and '*'
5+
'?' matches any single character
6+
'*' matches zero or more characters
7+
8+
*/
9+
10+
//Bottom Up Dynamic Programming
11+
12+
public boolean isMatch(String str, String pattern){
13+
char s[] = str.toCharArray();
14+
char p[] = pattern.toCharArray();
15+
16+
//match[i][j] indicates if str[0...i] matches with pattern[0...j]
17+
boolean match[][] = new boolean[str.length() + 1][pattern.length() + 1];
18+
19+
//empty string matches with empty pattern
20+
match[0][0] = true;
21+
22+
//when the string is empty, only one character can match it ie.'*'
23+
//check if the first element of the pattern is '*' to match with an empty string
24+
if(pattern.length() > 0 && p[0] == '*') match[0][1] = true;
25+
26+
for(int i=1 ; i < str.length() ; i++){
27+
for(int j=1 ; j < pattern.length() ; j++){
28+
29+
//If the current elements of the pattern and the string match, we check the result of the subproblems
30+
//the subproblem in this case is --> string and pattern without considering the current elements
31+
//Also, if current pattern element is '?', it can match with any ONE character of the string.
32+
//the subproblem again becomes the same
33+
if(p[j-1] == s[i-1] || p[j-1]=='?'){
34+
match[i][j] = match[i-1][j-1];
35+
}
36+
37+
//'*' matches with 0 or more characters
38+
//when we consider '*' to match with 0 characters, we basically imply '*' doesn't exsist in the pattern -->
39+
//subproblem becomes match[i][j-1]
40+
//'*' matches with one or more characters
41+
//consider '*' to match with current character, we need to find out does '*' match with the remaining characters of
42+
//the string?? --> subproblem becomes match[i-1][j]
43+
else if(p[j-1] == '*'){
44+
match[i][j] = (match[i-1][j] || match[i][j-1]);
45+
}
46+
}
47+
}
48+
49+
return match[str.length()][pattern.length()];
50+
}
51+
52+
//Bottom up Optimized Approach
53+
/*
54+
Since the pattern might contain a long chain of continuous '*'s, we will reduce the pattern size.
55+
multiple '*'s are equaivalent to a single '*'
56+
*/
57+
58+
public boolean isMatch(String str, String pattern){
59+
char s[] = str.toCharArray();
60+
char p[] = pattern.toCharArray();
61+
62+
int index = 0;
63+
boolean isFirst = true;
64+
for(int i=0 ; i < p.length ; i++){
65+
if(p[i] == '*'){
66+
if(isFirst){
67+
p[index++] = p[i];
68+
isFirst = false;
69+
}
70+
}
71+
else{
72+
p[index++] = p[i];
73+
isFirst = false;
74+
}
75+
}
76+
77+
boolean match[][] = new boolean[s.length + 1][index + 1];
78+
79+
match[0][0] = true;
80+
81+
if(index > 0 && p[0] == '*') match[0][1] = true;
82+
83+
for(int i=1 ; i < str.length() ; i++){
84+
for(int j=1 ; j < match[0].length ; j++){
85+
86+
if(p[j-1] == s[i-1] || p[j-1]=='?'){
87+
match[i][j] = match[i-1][j-1];
88+
}
89+
90+
else if(p[j-1] == '*'){
91+
match[i][j] = (match[i-1][j] || match[i][j-1]);
92+
}
93+
}
94+
}
95+
96+
return match[s.length][index];
97+
98+
}
99+
100+
//This code can be further optimized - Instead of using a (n*m) matrix we can use a (2*m) matrix
101+
//where n = length(string) and m = length(pattern)
102+
103+
104+
/*
105+
Time and Space Complexity - O(n*m)
106+
*/
107+

0 commit comments

Comments
 (0)