leetcode 10. Regular Expression Matching

来源:互联网 发布:自然语言处理的算法 编辑:程序博客网 时间:2024/05/06 11:08

题目


Implement regular expression matching with support for '.' and '*'.




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


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


The function prototype should be:
bool isMatch(const char *s, const char *p)


Some examples:
isMatch("aa","a") → false
isMatch("aa","aa") → true
isMatch("aaa","aa") → false
isMatch("aa", "a*") → true
isMatch("aa", ".*") → true
isMatch("ab", ".*") → true
isMatch("aab", "c*a*b") → true


解析
当模式中的第二个字符不是“*”时:
1、如果字符串第一个字符和模式中的第一个字符相匹配,那么字符串和模式都后移一个字符,然后匹配剩余的。
2、如果 字符串第一个字符和模式中的第一个字符相不匹配,直接返回false。


而当模式中的第二个字符是“*”时:
如果字符串第一个字符跟模式第一个字符不匹配,则模式后移2个字符,继续匹配。如果字符串第一个字符跟模式第一个字符匹配,可以有3种匹配方式:
1、模式后移2字符,相当于x*被忽略;
2、字符串后移1字符,模式后移2字符;
3、字符串后移1字符,模式不变,即继续匹配字符下一位,因为*可以匹配多位;


public class Solution {    public boolean match(char[] str, char[] pattern)    {        if (str == null || pattern == null)            return false;        int strIndex = 0;        int patternIndex = 0;        return matchCore(str, strIndex, pattern, patternIndex);    }     public boolean matchCore(char[] str, int strIndex, char[] pattern, int patternIndex) {        if (str.length == strIndex && patternIndex == pattern.length)            return true;        if (strIndex != str.length && pattern.length == patternIndex)            return false;         //模式第二个是*,且字符串第一个和模式第一个匹配,分三种情况;否则模式后移。        if (patternIndex + 1 < pattern.length && pattern[patternIndex + 1] == '*') {            if (strIndex != str.length && str[strIndex] == pattern[patternIndex]               || (pattern[patternIndex] == '.' && strIndex != str.length)) {                return matchCore(str, strIndex, pattern, patternIndex + 2)                        || matchCore(str, strIndex + 1, pattern, patternIndex + 2)                        || matchCore(str, strIndex + 1, pattern, patternIndex);            } else                return matchCore(str, strIndex, pattern, patternIndex + 2);        }          //模式第2个不是*,且字符串第1个跟模式第1个匹配,则都后移1位,否则直接返回false        if ((strIndex != str.length && str[strIndex] == pattern[patternIndex])                || (strIndex != str.length && pattern[patternIndex] == '.')) {            return matchCore(str, strIndex + 1, pattern, patternIndex + 1);        }        return false;    }}

This Solution use 2D DP. beat 90% solutions, very simple.

Here are some conditions to figure out, then the logic can be very straightforward.

1, If p.charAt(j) == s.charAt(i) :  dp[i][j] = dp[i-1][j-1];2, If p.charAt(j) == '.' : dp[i][j] = dp[i-1][j-1];3, If p.charAt(j) == '*':    here are two sub conditions:               1   if p.charAt(j-1) != s.charAt(i) : dp[i][j] = dp[i][j-2]  //in this case, a* only counts as empty               2   if p.charAt(i-1) == s.charAt(i) or p.charAt(i-1) == '.':                              dp[i][j] = dp[i-1][j]    //in this case, a* counts as multiple a                            or dp[i][j] = dp[i][j-1]   // in this case, a* counts as single a                           or dp[i][j] = dp[i][j-2]   // in this case, a* counts as empty
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 i = 0; i < p.length(); i++) {        if (p.charAt(i) == '*' && dp[0][i-1]) {            dp[0][i+1] = true;        }    }    for (int i = 0 ; i < s.length(); i++) {        for (int j = 0; j < p.length(); j++) {            if (p.charAt(j) == '.') {                dp[i+1][j+1] = dp[i][j];            }            if (p.charAt(j) == s.charAt(i)) {                dp[i+1][j+1] = dp[i][j];            }            if (p.charAt(j) == '*') {                if (p.charAt(j-1) != s.charAt(i) && p.charAt(j-1) != '.') {                    dp[i+1][j+1] = dp[i+1][j-1];                } else {                    dp[i+1][j+1] = (dp[i+1][j] || dp[i][j+1] || dp[i+1][j-1]);                }            }        }    }    return dp[s.length()][p.length()];}

原创粉丝点击