Skip to content

Commit 6037dbf

Browse files
Add Regular Expression Matching
1 parent 35168be commit 6037dbf

File tree

3 files changed

+78
-2
lines changed

3 files changed

+78
-2
lines changed

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ This repository contains my solutions to various LeetCode problems. Each solutio
77

88
## LeetCode Stats
99
- **Profile**: [Vinnn_96](https://leetcode.com/u/Vinnn_96/)
10-
- **Problems Solved**: 11
10+
- **Problems Solved**: 12
1111
- **Languages**: Java
1212

1313
## Repository Structure
@@ -29,6 +29,7 @@ Each problem has its own directory containing:
2929
| 7 | [Reverse Integer](Reverse%20Integer) | Medium | [Java](Reverse%20Integer/Solution.java) | Math |
3030
| 8 | [String to Integer (atoi)](./String%20to%20Integer%20(atoi)) | Medium | [Java](./String%20to%20Integer%20(atoi)/Solution.java) | String |
3131
| 9 | [Palindrome Number](Palindrome%20Number) | Easy | [Java](Palindrome%20Number/Solution.java) | Math |
32+
| 10 | [Regular Expression Matching](Regular%20Expression%20Matching) | Hard | [Java](Regular%20Expression%20Matching/Solution.java) | String, Dynamic Programming, Recursion |
3233
| 214 | [Shortest Palindrome](Shortest%20Palindrome) | Hard | [Java](Shortest%20Palindrome/Solution.java) | String, Rolling Hash, String Matching, Hash Function |
3334
| 2924 | [Find Champion II](Find%20Champion%20II) | Medium | [Java](Find%20Champion%20II/Solution.java) | Graph |
3435

@@ -37,7 +38,7 @@ Each problem has its own directory containing:
3738
### By Difficulty
3839
- Easy: 2
3940
- Medium: 7
40-
- Hard: 2
41+
- Hard: 3
4142

4243
### By Topics
4344
- Array Problems
@@ -52,8 +53,10 @@ Each problem has its own directory containing:
5253
- [Zigzag Conversion](Zigzag%20Conversion)
5354
- [Shortest Palindrome](Shortest%20Palindrome)
5455
- [String to Integer (atoi)](String%20to%20Integer%20(atoi))
56+
- [Regular Expression Matching](Regular%20Expression%20Matching)
5557
- Dynamic Programming Problems
5658
- [Longest Palindromic Substring](Longest%20Palindromic%20Substring)
59+
- [Regular Expression Matching](Regular%20Expression%20Matching)
5760
- Linked List Problems
5861
- [Add Two Numbers](Add%20Two%20Numbers)
5962
- Math Problems
@@ -66,6 +69,8 @@ Each problem has its own directory containing:
6669
- [Find Champion II](Find%20Champion%20II)
6770
- String Matching Problems
6871
- [Shortest Palindrome](Shortest%20Palindrome)
72+
- Recursion Problems
73+
- [Regular Expression Matching](Regular%20Expression%20Matching)
6974

7075
## About LeetCode
7176

Regular Expression Matching/README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Regular Expression Matching
2+
3+
## LeetCode Problem
4+
- Problem Number: 10
5+
- Problem Link: https://leetcode.com/problems/regular-expression-matching/
6+
- Difficulty: Hard
7+
- Topics: String, Dynamic Programming, Recursion
8+
9+
## Problem Description
10+
Given an input string s and a pattern p, implement regular expression matching with support for '.' and '*' where:
11+
- '.' Matches any single character
12+
- '*' Matches zero or more of the preceding element
13+
14+
The matching should cover the entire input string (not partial).
15+
16+
Note:
17+
- For each appearance of '*', there will be a previous valid character to match
18+
- The pattern must match the entire string, not just a portion of it
19+
20+
## Examples
21+
22+
### Example 1:
23+
```
24+
Input: s = "aa", p = "a"
25+
Output: false
26+
Explanation: "a" does not match the entire string "aa".
27+
```
28+
29+
### Example 2:
30+
```
31+
Input: s = "aa", p = "a*"
32+
Output: true
33+
Explanation: '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
34+
```
35+
36+
### Example 3:
37+
```
38+
Input: s = "ab", p = ".*"
39+
Output: true
40+
Explanation: ".*" means "zero or more (*) of any character (.)".
41+
```
42+
43+
## Constraints
44+
- 1 <= s.length <= 20
45+
- 1 <= p.length <= 20
46+
- s contains only lowercase English letters
47+
- p contains only lowercase English letters, '.', and '*'
48+
- It is guaranteed for each appearance of the character '*', there will be a previous valid character to match.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public boolean isMatch(String s, String p) {
3+
return isMatchHelper(s, p, 0, 0, new Boolean[s.length() + 1][p.length() + 1]);
4+
}
5+
6+
private boolean isMatchHelper(String s, String p, int i, int j, Boolean[][] memo) {
7+
if (memo[i][j] != null) {
8+
return memo[i][j];
9+
}
10+
if (j == p.length()) {
11+
return memo[i][j] = (i == s.length());
12+
}
13+
boolean firstMatch = (i < s.length() &&
14+
(s.charAt(i) == p.charAt(j) || p.charAt(j) == '.'));
15+
if (j + 1 < p.length() && p.charAt(j + 1) == '*') {
16+
memo[i][j] = isMatchHelper(s, p, i, j + 2, memo) ||
17+
(firstMatch && isMatchHelper(s, p, i + 1, j, memo));
18+
} else {
19+
memo[i][j] = firstMatch && isMatchHelper(s, p, i + 1, j + 1, memo);
20+
}
21+
return memo[i][j];
22+
}
23+
}

0 commit comments

Comments
 (0)