[LeetCode] 10. Regular Expression Matching

Problem

Given an input string (s) and a pattern (p), implement regular expression matching with support for '.' and '*'.

1
2
'.' Matches any single character.
'*' Matches zero or more of the preceding element.

The matching should cover the entire input string (not partial).

Note:

  • s could be empty and contains only lowercase letters a-z.
  • p could be empty and contains only lowercase letters a-z, and characters like . or *.

Example 1:

1
2
3
4
5
Input:
s = "aa"
p = "a"
Output: false
Explanation: "a" does not match the entire string "aa".

Example 2:

1
2
3
4
5
Input:
s = "aa"
p = "a*"
Output: true
Explanation: '*' means zero or more of the precedeng element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".

Example 3:

1
2
3
4
5
Input:
s = "ab"
p = ".*"
Output: true
Explanation: ".*" means "zero or more (*) of any character (.)".

Example 4:

1
2
3
4
5
Input:
s = "aab"
p = "c*a*b"
Output: true
Explanation: c can be repeated 0 times, a can be repeated 1 time. Therefore it matches "aab".

Example 5:

1
2
3
4
Input:
s = "mississippi"
p = "mis*is*p*."
Output: false

Explanation

  1. If p is empty, check if s is also empty.

  2. If p has length 1, check if s is also has length 1 and their first character is match.

  3. If p’s second character is not *, check if their first character is match and recursivly check the strings starting from the second character of s and starting from the second character of p.

  4. If p’s second character is *. While s is not empty and p and s’s first character match, if s and starting from the third character of p match return true, else remove the first character of s and do the while loop again.

    1
    2
     s = "ab"
     p = "a*ab"
    

    or

    1
    2
     s = "b"
     p = "a*b"
    
    • From the above example, the case is ` * ` repeat its previous character 0 times or the first character are not match. If the first character are matched, then we check s with p that starts from the third character, in other words, compare “ab” from s and “ab” from p. If the first character are not matched, then we compare “b” from s and “b” from p.
    1
    2
     s = "aab"
     p = "a*b"
    
    • From the above example, the case is ` * ` repeats its previous character at least one time. After we compare their first character matches, we remove the first character of s and compare with p again, which is s = a b and p = a * b.
  5. After the while loop, check s with p starting from the third character.

    1
    2
     s = "aaab"
     p = "a*b"
    

    or

    1
    2
     s = "xb"
     p = "a*b"
    
    • From the above example, we check the last b from s and the third character of p, which is b.

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
    public boolean isMatch(String s, String p) {
        if (p.isEmpty()) return s.isEmpty();

        if (p.length() == 1) {
            return s.length() == 1 &&
                (p.charAt(0) == '.' || p.charAt(0) == s.charAt(0));
        }

        if (p.charAt(1) != '*') {
            if (s.isEmpty()) return false;
            return (p.charAt(0) == '.' || p.charAt(0) == s.charAt(0)) &&
                isMatch(s.substring(1), p.substring(1));
        }

        while (!s.isEmpty() && (p.charAt(0) == '.' || p.charAt(0)==s.charAt(0))) {
            if (isMatch(s, p.substring(2))) return true;
            s = s.substring(1);
        }

        return isMatch(s, p.substring(2));
    }
}