Skip to content

Commit 566c27a

Browse files
authored
WildcardMatching Added (#4404)
* Added WildcardMatching DP * Wildcard Matching update * Updated WildcardMatching * Added WildcardMatchingTests * WildcardMatching update * Clang-formatting done * WildcardMatching_Clang-formatting done * WildcardMatching
1 parent 1cf193c commit 566c27a

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
*
3+
* Author: Janmesh Singh
4+
* Github: https://github.com/janmeshjs
5+
6+
* Problem Statement: To determine if the pattern matches the text.
7+
* The pattern can include two special wildcard characters:
8+
* ' ? ': Matches any single character.
9+
* ' * ': Matches zero or more of any character sequence.
10+
*
11+
* Use DP to return True if the pattern matches the entire text and False otherwise
12+
*
13+
*/
14+
15+
package com.thealgorithms.dynamicprogramming;
16+
17+
public class WildcardMatching {
18+
19+
public static boolean isMatch(String text, String pattern) {
20+
int m = text.length();
21+
int n = pattern.length();
22+
23+
// Create a DP table to store intermediate results
24+
boolean[][] dp = new boolean[m + 1][n + 1];
25+
26+
// Base case: an empty pattern matches an empty text
27+
dp[0][0] = true;
28+
29+
// Handle patterns starting with '*'
30+
for (int j = 1; j <= n; j++) {
31+
if (pattern.charAt(j - 1) == '*') {
32+
dp[0][j] = dp[0][j - 1];
33+
}
34+
}
35+
36+
// Fill the DP table
37+
for (int i = 1; i <= m; i++) {
38+
for (int j = 1; j <= n; j++) {
39+
char textChar = text.charAt(i - 1);
40+
char patternChar = pattern.charAt(j - 1);
41+
42+
if (patternChar == textChar || patternChar == '?') {
43+
dp[i][j] = dp[i - 1][j - 1];
44+
} else if (patternChar == '*') {
45+
// '*' can match zero or more characters
46+
dp[i][j] = dp[i - 1][j] || dp[i][j - 1];
47+
} else {
48+
dp[i][j] = false;
49+
}
50+
}
51+
}
52+
// The result is in the bottom-right cell of the DP table
53+
return dp[m][n];
54+
}
55+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.thealgorithms.dynamicprogramming;
2+
import static org.junit.jupiter.api.Assertions.*;
3+
4+
import org.junit.jupiter.api.Test;
5+
6+
public class WildcardMatchingTest {
7+
8+
@Test
9+
public void testMatchingPattern() {
10+
assertTrue(WildcardMatching.isMatch("aa", "a*"));
11+
assertTrue(WildcardMatching.isMatch("adceb", "*a*b"));
12+
}
13+
14+
@Test
15+
public void testNonMatchingPattern() {
16+
assertFalse(WildcardMatching.isMatch("cb", "?a"));
17+
assertFalse(WildcardMatching.isMatch("acdcb", "a*c?b"));
18+
assertFalse(WildcardMatching.isMatch("mississippi", "m*issi*iss?*i"));
19+
}
20+
21+
@Test
22+
public void testEmptyPattern() {
23+
assertTrue(WildcardMatching.isMatch("", ""));
24+
assertFalse(WildcardMatching.isMatch("abc", ""));
25+
}
26+
}

0 commit comments

Comments
 (0)