首页 > 其他分享 >leetcode 10 - Regular Expression Matching - hard

leetcode 10 - Regular Expression Matching - hard

时间:2022-11-28 09:35:41浏览次数:48  
标签:aa 10 Explanation hard character Example match Matching dp

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

  • '.' Matches any single character.​​​​
  • '*' Matches zero or more of the preceding element.

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

 

Example 1:

Input: s = "aa", p = "a"
Output: false
Explanation: "a" does not match the entire string "aa".

Example 2:

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

Example 3:

Input: s = "ab", p = ".*"
Output: true
Explanation: ".*" means "zero or more (*) of any character (.)".

 

Constraints:

  • 1 <= s.length <= 20
  • 1 <= p.length <= 30
  • s contains only lowercase English letters.
  • p contains only lowercase English letters, '.', and '*'.
  • It is guaranteed for each appearance of the character '*', there will be a previous valid character to match.

 

Example 1:

Input: s = "aa", p = "a"
Output: false
Explanation: "a" does not match the entire string "aa".

Example 2:

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

Example 3:

Input: s = "ab", p = ".*"
Output: true
Explanation: ".*" means "zero or more (*) of any character (.)".

 

Constraints:

  • 1 <= s.length <= 20
  • 1 <= p.length <= 30
  • s contains only lowercase English letters.
  • p contains only lowercase English letters, '.', and '*'.
  • It is guaranteed for each appearance of the character '*', there will be a previous valid character to match.

 

涉及两个string match的问题,一般都是动态规划。

State: dp[i][j] i,j是字符数,总共[len(s)+1][len(p)+1]。s中取前i个字符(s[i-1])和p中前j个字符是否match

初始化:p为空,s只有是空字符才是true;s为空,dp[0][0]=T, dp[0][1]=F, 往下每个, 因为可以引入*了,取决于dp[0][j-2]

转移方程:1. if s[i-1] match p[j-1], 当前值等于左上角的值. 2. 当前*,从上往下取i-2的值(abcd, abcde*),从左往右,取i-1的值(abcddd, abcdddd => abcd*)

Result: dp[len(s)][len(p)]

 

实现:

class Solution {
    public boolean isMatch(String s, String p) {

        if (s == null || p == null) return false;

        boolean[][] dp = new boolean[s.length()+1][p.length()+1];
        dp[0][0] = true;
        
        for (int j = 1; j <= p.length(); j++) {
            if (p.charAt(j-1) == '*') {
                dp[0][j] = dp[0][j-2];
            }
        }

        for (int i = 1; i <= s.length(); i++) {
            for (int j = 1; j<=p.length(); j++) {
                if (p.charAt(j-1) == '.' || p.charAt(j-1) == s.charAt(i-1)) {
                    dp[i][j] = dp[i-1][j-1];
                } else if (p.charAt(j-1) == '*') {
                    if (p.charAt(j-2) == s.charAt(i-1) || p.charAt(j-2) == '.') {
                        dp[i][j] = dp[i][j-2] || dp[i-1][j];
                    } else {
                        dp[i][j] = dp[i][j-2];
                    }
                }
            }
        }
        
        return dp[s.length()][p.length()];

    }
}

 

标签:aa,10,Explanation,hard,character,Example,match,Matching,dp
From: https://www.cnblogs.com/xuningwang/p/16931343.html

相关文章