-
Notifications
You must be signed in to change notification settings - Fork 822
/
Copy pathWildcardMatching.java
69 lines (66 loc) · 2.43 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
/* (C) 2024 YourCompanyName */
package backtracking;
/**
* Created by gouthamvidyapradhan on 21/01/2018. Implement wildcard pattern matching with support
* for '?' and '*'.
*
* <p>'?' Matches any single character. '*' Matches any sequence of characters (including the empty
* sequence).
*
* <p>The matching should cover the entire input string (not partial).
*
* <p>The function prototype should be: bool isMatch(const char *s, const char *p)
*
* <p>Some examples: isMatch("aa","a") → false isMatch("aa","aa") → true isMatch("aaa","aa") → false
* isMatch("aa", "*") → true isMatch("aa", "a*") → true isMatch("ab", "?*") → true isMatch("aab",
* "c*a*b") → false
*
* <p>Solution: Maintain two indexes one for string and other one for the pattern. 1. If the
* characters match in both the indexes or if the char at pattern is '?' then increment both the
* indexes. 2. If a star(*) is encountered save the position of star in the given string as
* 'startPosAtStr' and position of star in the pattern as 'starIdx' and this time increment only
* index for pattern. 3. If the characters do not match and if the start is not encountered
* previously return false else increment 'startPosAtStr' and assign this as the new index for
* string and start the new pattern index from starIdx + 1
*/
public class WildcardMatching {
/**
* Main method
*
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
System.out.println(new WildcardMatching().isMatch("abebd", "?be******e"));
}
public boolean isMatch(String s, String p) {
int starIdx = -1;
int starPosAtStr = -1;
int j = 0;
for (int i = 0, l = s.length(); i < l; ) {
if (j < p.length()) {
if (s.charAt(i) == p.charAt(j) || p.charAt(j) == '?') {
i++;
j++;
} else if (p.charAt(j) == '*') {
starIdx = j;
starPosAtStr = i;
j++; // increment only pattern index. This is because '*' can match also empty string.
} else if (starIdx != -1) {
i = ++starPosAtStr;
j = starIdx + 1;
} else return false;
} else if (starIdx != -1) {
i = ++starPosAtStr;
j = starIdx + 1;
} else return false;
}
// check if the remaining characters in pattern contains only '*'
while (j < p.length()) {
if (p.charAt(j) == '*') {
j++;
} else break;
}
return j == p.length();
}
}