-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution10.cs
32 lines (28 loc) · 1.08 KB
/
Solution10.cs
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
namespace LeetCode.Solutions;
public class Solution10
{
/// <summary>
/// Problem #10
/// <a href="https://leetcode.com/problems/regular-expression-matching/">See the problem</a>
/// </summary>
public bool IsMatch(string s, string p)
{
// Base case: if the pattern is empty, return true if the string is also empty
if (string.IsNullOrEmpty(p))
{
return string.IsNullOrEmpty(s);
}
// First character match flag (considering '.' wildcard)
var firstMatch = !string.IsNullOrEmpty(s) && (s[0] == p[0] || p[0] == '.');
// If the pattern has a '*' character
if (p.Length >= 2 && p[1] == '*') {
// Two possibilities:
// 1. '*' acts as zero occurrence
// 2. '*' acts as one or more occurrence (first character must match)
return IsMatch(s, p[2..]) ||
(firstMatch && IsMatch(s[1..], p));
}
// If no '*', just move to the next character in both string and pattern
return firstMatch && IsMatch(s[1..], p[1..]);
}
}