-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathProblem_39.java
More file actions
52 lines (44 loc) · 2.04 KB
/
Problem_39.java
File metadata and controls
52 lines (44 loc) · 2.04 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
package strings;
// String matching where one string contains wildcard characters
public class Problem_39 {
public static boolean isMatch(String text, String pattern) {
int textIndex = 0;
int patternIndex = 0;
// Track the last matched position of '*' in the pattern
int starIndex = -1;
int i = 0;
while (textIndex < text.length()) {
char textChar = text.charAt(textIndex);
char patternChar = pattern.charAt(patternIndex);
// If the characters match or the pattern character is '?' (match any character)
if (textChar == patternChar || patternChar == '?') {
textIndex++;
patternIndex++;
} else if (patternChar == '*') {
// '*' can match any sequence of characters (including empty sequence)
starIndex = patternIndex;
i = textIndex; // Mark the position to backtrack from if needed
patternIndex++; // Move on in the pattern
} else if (starIndex != -1) {
// Mismatch after '*', try backtracking
patternIndex = starIndex + 1; // Move to the character after '*'
i++; // Try matching the next character in the text
textIndex = i; // Backtrack to the position after the last match with '*'
} else {
// No match, return false
return false;
}
}
// Handle remaining characters in the pattern after reaching the end of the text
while (patternIndex < pattern.length() && pattern.charAt(patternIndex) == '*') {
patternIndex++;
}
return patternIndex == pattern.length(); // Return true if all characters in the pattern are matched
}
public static void main(String[] args) {
String text = "baaabab";
String pattern = "ba*a*b*ab";
boolean isMatch = isMatch(text, pattern);
System.out.println("Text '" + text + "' matches pattern '" + pattern + "'? " + isMatch);
}
}