Skip to content

Commit fbd0f5d

Browse files
authored
Enhance README with solution steps and implementation
Added detailed steps for solving the Regular Expression Matching problem in Java, including the implementation of the 'isMatch' method.
1 parent 48fe3c2 commit fbd0f5d

File tree

1 file changed

+64
-1
lines changed
  • src/main/java/g0001_0100/s0010_regular_expression_matching

1 file changed

+64
-1
lines changed

src/main/java/g0001_0100/s0010_regular_expression_matching/readme.md

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,67 @@ The matching should cover the **entire** input string (not partial).
3939
* `1 <= p.length <= 20`
4040
* `s` contains only lowercase English letters.
4141
* `p` contains only lowercase English letters, `'.'`, and `'*'`.
42-
* It is guaranteed for each appearance of the character `'*'`, there will be a previous valid character to match.
42+
* It is guaranteed for each appearance of the character `'*'`, there will be a previous valid character to match.
43+
44+
To solve the Regular Expression Matching problem in Java using a `Solution` class, we'll follow these steps:
45+
46+
1. Define a `Solution` class with a method named `isMatch`.
47+
2. Implement a recursive approach to check for pattern matching.
48+
3. Base cases:
49+
- If the pattern string is empty, return `s.isEmpty()`.
50+
- If the pattern string's length is 1 or the next character after `*` is `.`:
51+
- Check if the length of `s` is 1 and the characters match or the pattern is `.`.
52+
- If so, return `true`; otherwise, return `false`.
53+
4. If the second character of the pattern is not `*`, recursively call `isMatch` with the substring starting from the second character.
54+
5. If the second character of the pattern is `*`, recursively check all possibilities:
55+
- Zero occurrences of the preceding character (skipping `*` and the character before it).
56+
- One or more occurrences of the preceding character (matching the first character and recursively calling `isMatch` for the remaining part of the string).
57+
6. Return the result of the recursive checks.
58+
7. Handle edge cases where the input strings are empty or the pattern contains invalid characters.
59+
60+
Here's the implementation:
61+
62+
```java
63+
public class Solution {
64+
65+
public boolean isMatch(String s, String p) {
66+
if (p.isEmpty())
67+
return s.isEmpty();
68+
69+
boolean firstMatch = !s.isEmpty() && (p.charAt(0) == s.charAt(0) || p.charAt(0) == '.');
70+
71+
if (p.length() >= 2 && p.charAt(1) == '*') {
72+
return isMatch(s, p.substring(2)) || (firstMatch && isMatch(s.substring(1), p));
73+
} else {
74+
return firstMatch && isMatch(s.substring(1), p.substring(1));
75+
}
76+
}
77+
78+
public static void main(String[] args) {
79+
Solution solution = new Solution();
80+
81+
// Test cases
82+
String s1 = "aa";
83+
String p1 = "a";
84+
System.out.println("Example 1 Output: " + solution.isMatch(s1, p1));
85+
86+
String s2 = "aa";
87+
String p2 = "a*";
88+
System.out.println("Example 2 Output: " + solution.isMatch(s2, p2));
89+
90+
String s3 = "ab";
91+
String p3 = ".*";
92+
System.out.println("Example 3 Output: " + solution.isMatch(s3, p3));
93+
94+
String s4 = "aab";
95+
String p4 = "c*a*b";
96+
System.out.println("Example 4 Output: " + solution.isMatch(s4, p4));
97+
98+
String s5 = "mississippi";
99+
String p5 = "mis*is*p*.";
100+
System.out.println("Example 5 Output: " + solution.isMatch(s5, p5));
101+
}
102+
}
103+
```
104+
105+
This implementation provides a solution to the Regular Expression Matching problem in Java.

0 commit comments

Comments
 (0)