Skip to content

Commit 312b032

Browse files
committed
Updated wildcard matching
1 parent e7a1841 commit 312b032

File tree

1 file changed

+41
-76
lines changed

1 file changed

+41
-76
lines changed

Hard/WildcardMatching.java

Lines changed: 41 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -25,99 +25,64 @@
2525
*/
2626
class WildcardMatching {
2727
public static void main(String[] args) {
28+
WildcardMatching w = new WildcardMatching();
2829
/*input validation*/
29-
// System.out.println(isMatch("", "a")); // false
30-
// System.out.println(isMatch("a", "")); // false
31-
// System.out.println(isMatch("", "")); // true
32-
// System.out.println(isMatch(null, null)); // true
33-
// System.out.println(isMatch("a", null)); // false
34-
// System.out.println(isMatch(null, "null")); // false
35-
/*letters*/
36-
// System.out.println(isMatch("a", "aa")); // false
37-
// System.out.println(isMatch("aa", "a")); // false
38-
// System.out.println(isMatch("aa", "aa")); // true
39-
// System.out.println(isMatch("aa", "ab")); // false
40-
/**s*/
41-
// System.out.println(isMatch("aa", "*")); // true
42-
/*?s*/
43-
// System.out.println(isMatch("aa", "?")); // false
44-
// System.out.println(isMatch("a", "?")); // true
45-
/*letters and *s*/
46-
// System.out.println(isMatch("abc", "a*")); // true
47-
// System.out.println(isMatch("ab", "a*")); // true
48-
// System.out.println(isMatch("ab", "*a")); // false
49-
// System.out.println(isMatch("a", "a*")); // true
50-
// System.out.println(isMatch("bcsa", "*a")); // true
51-
// System.out.println(isMatch("bcs", "*a")); // false
52-
// System.out.println(isMatch("bbbbbbbbbb", "*bbbbb")); // true
30+
// System.out.println(w.isMatch("", "a")); // false
31+
// System.out.println(w.isMatch("a", "")); // false
32+
// System.out.println(w.isMatch("", "")); // true
33+
// System.out.println(w.isMatch(null, null)); // true
34+
// System.out.println(w.isMatch("a", null)); // false
35+
// System.out.println(w.isMatch(null, "null")); // false
36+
/*letters*/
37+
// System.out.println(w.isMatch("a", "aa")); // false
38+
// System.out.println(w.isMatch("aa", "a")); // false
39+
// System.out.println(w.isMatch("aa", "aa")); // true
40+
// System.out.println(w.isMatch("aa", "ab")); // false
41+
/**s*/
42+
// System.out.println(w.isMatch("aa", "*")); // true
43+
/*?s*/
44+
// System.out.println(w.isMatch("aa", "?")); // false
45+
// System.out.println(w.isMatch("a", "?")); // true
46+
/*letters and *s*/
47+
// System.out.println(w.isMatch("abc", "a*")); // true
48+
// System.out.println(w.isMatch("ab", "a*")); // true
49+
// System.out.println(w.isMatch("ab", "*a")); // false
50+
// System.out.println(w.isMatch("a", "a*")); // true
51+
// System.out.println(w.isMatch("bcsa", "*a")); // true
52+
// System.out.println(w.isMatch("bcs", "*a")); // false
53+
// System.out.println(w.isMatch("bbbbbbbbbb", "*bbbbb")); // true
5354

5455
/* * and ? */
5556
// System.out.println(isMatch("b", "*?*?")); // false
5657
}
5758

5859
/**
59-
* Two pointers
60+
* DP, two pointers
6061
* remember the index of * and matched sequence
6162
* advance only pattern pointer when * is found
6263
* match the sequence after * in pattern with the rest of the string
6364
*/
64-
public static boolean isMatch(String str, String pattern) {
65+
public boolean isMatch(String str, String pattern) {
6566
if (str == null && pattern == null) return true;
6667
if (str == null || pattern == null) return false;
68+
6769
int s = 0, p = 0, match = 0, astroIdx = -1;
68-
while (s < str.length()){
69-
// move both pointers
70-
if (p < pattern.length() && (pattern.charAt(p) == '?' || str.charAt(s) == pattern.charAt(p))){
71-
s++;
70+
while (s < str.length()) {
71+
if (p < pattern.length() && (pattern.charAt(p) == '?' || str.charAt(s) == pattern.charAt(p))){ // found ? or same chars
72+
s++; // move both pointers
7273
p++;
73-
}
74-
// * found, only move pattern pointer
75-
else if (p < pattern.length() && pattern.charAt(p) == '*'){
76-
astroIdx = p;
77-
match = s;
78-
p++;
79-
}
80-
// last pattern pointer was *, move string pointer
81-
else if (astroIdx != -1){
82-
p = astroIdx + 1; // move to next of *
83-
match++;
74+
} else if (p < pattern.length() && pattern.charAt(p) == '*') { // found *
75+
astroIdx = p; // save astroid index in pattern
76+
match = s; // save current index of string
77+
p++; // only move pattern pointer forward
78+
} else if (astroIdx != -1){ // try to find last astroid
79+
p = astroIdx + 1; // move to * one char behind astroid
80+
match++; // move current index of string
8481
s = match;
85-
}
86-
//current pattern pointer is not star, last patter pointer was not *
87-
//characters do not match
88-
else return false;
82+
} else return false; // not ?, not same char, not *, don't match
8983
}
90-
91-
//check for remaining characters in pattern
84+
// check remaining characters in pattern, can only be astroid
9285
while (p < pattern.length() && pattern.charAt(p) == '*') p++;
93-
return p == pattern.length();
94-
}
95-
96-
/**
97-
* validate input
98-
* seperate letter, * and ?
99-
* how can be a match? letters only, * only, ? only
100-
* letters and *, letters and ?, * and ?
101-
* letters, * and ?
102-
* check current char, return the result of current and latter ones
103-
*/
104-
public static boolean isMatch(String s, String p) {
105-
// System.out.println(s + " | " + p);
106-
if (s == null && p == null) return true;
107-
if (s == null || p == null) return false;
108-
if (s.equals("") || p.equals("")) return hasAstro || p.equals("*") || p.equals(s);
109-
for (int i = 0; i < p.length(); i++) {
110-
char fromP = p.charAt(i);
111-
char fromS = s.charAt(i);
112-
if (fromP == '*') { // problem here with astroid
113-
return isMatch(s, p.substring(i + 1));
114-
} else if (fromP == '?'|| fromP == fromS) {
115-
return isMatch(s.substring(i + 1), p.substring(i + 1));
116-
} else {
117-
return false;
118-
}
119-
}
120-
return true;
86+
return p == pattern.length(); // no remaining
12187
}
122-
12388
}

0 commit comments

Comments
 (0)