forked from admirerr/DSA-Collection
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWildcardMatching.java
More file actions
58 lines (49 loc) · 2.06 KB
/
WildcardMatching.java
File metadata and controls
58 lines (49 loc) · 2.06 KB
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
public class WildcardMatching {
// Function to check if string s matches pattern p
public boolean isMatch(String s, String p) {
int n = s.length();
int m = p.length();
// DP table: dp[i][j] = does s[0..i-1] match p[0..j-1] ?
boolean[][] dp = new boolean[n + 1][m + 1];
// Base case: empty string matches empty pattern
dp[0][0] = true;
// Fill for patterns that start with '*'
// e.g., s = "" and p = "***" should be true
for (int j = 1; j <= m; j++) {
if (p.charAt(j - 1) == '*') {
dp[0][j] = dp[0][j - 1]; // '*' can represent empty string
}
}
// Fill the DP table
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
char sc = s.charAt(i - 1); // current char in string
char pc = p.charAt(j - 1); // current char in pattern
if (pc == sc || pc == '?') {
// If chars match OR '?' wildcard, carry forward diagonal
dp[i][j] = dp[i - 1][j - 1];
} else if (pc == '*') {
// '*' can represent:
// 1. empty sequence -> dp[i][j-1]
// 2. one or more characters -> dp[i-1][j]
dp[i][j] = dp[i][j - 1] || dp[i - 1][j];
} else {
dp[i][j] = false; // mismatch
}
}
}
// Answer is whether entire s matches entire p
return dp[n][m];
}
// ----------------------------
// Example Usage
// ----------------------------
public static void main(String[] args) {
WildcardMatching solver = new WildcardMatching();
System.out.println(solver.isMatch("aa", "a")); // false
System.out.println(solver.isMatch("aa", "*")); // true
System.out.println(solver.isMatch("cb", "?a")); // false
System.out.println(solver.isMatch("adceb", "*a*b")); // true
System.out.println(solver.isMatch("acdcb", "a*c?b")); // false
}
}