You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/main/java/g0001_0100/s0010_regular_expression_matching/readme.md
+64-1Lines changed: 64 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -39,4 +39,67 @@ The matching should cover the **entire** input string (not partial).
39
39
*`1 <= p.length <= 20`
40
40
*`s` contains only lowercase English letters.
41
41
*`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.
0 commit comments