LeetCode 10. Regular Expression Matching(正则表达式)

题目来源:https://leetcode.com/problems/regular-expression-matching/

问题描述

10. Regular Expression Matching

Hard

Given an input string (s) and a pattern (p), 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).

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:

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 precedeng 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 (.)".

Example 4:

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:

Input:

s = "mississippi"

p = "mis*is*p*."

Output: false

------------------------------------------------------------

题意

正则表达式匹配。其中’.’表示匹配一个任意字符,’x*’连起来表示x的任意次(包括0次)重复(x可以是a-z的字母,也可以是’.’,如果x是’.’则’.*’可以匹配任意字符串<包括空字符串>)。

------------------------------------------------------------

思路

难点在于对于’x*’的处理。

读入’x’时,判断紧跟在后面的字符是不是’*’,如果是,则转入’x*’的判断。对于’x*’,递归调用isMatch判断可能出现的各种匹配。

特别注意当原字符串s已经匹配完而模板字符串p仍有余的情况,这是如果p的余量都是’x*’的组合,则仍可能匹配成功。这种情况需要另外加以判断,同样可以用递归实现,也可以用循环实现。

------------------------------------------------------------

代码

class Solution {
    public boolean isMatch(String s, String p) {
        char sch = 0, pch = 0, pre = 0, next = 0;
        int n = s.length(), m = p.length(), i = 0, j = 0, k = 0;
        if (s.isEmpty() && p.isEmpty())
        {
            return true;
        }
        else if (!s.isEmpty() && p.isEmpty())
        {
            return false;
        }
        else if (s.isEmpty() && !p.isEmpty())
        {
            if (p.length() >= 2 && p.charAt(1) == '*')
            {
                return isMatch(s, p.substring(2));
            }
            else
            {
                return false;
            }
        }
        while (i < n && j < m)
        {
            sch = s.charAt(i);
            pch = p.charAt(j);
            if (pch >= 'a' && pch <= 'z')
            {
                if (j < m-1 && p.charAt(j+1) == '*')
                {
                    pre = pch;
                    j++;
                }
                else
                {
                    if (sch != pch)
                    {
                        return false;
                    }
                    else
                    {
                        pre = pch;
                        i++;
                        j++;
                    }
                }
            }
            else if (pch == '.')
            {
                if (j < m-1 && p.charAt(j+1) == '*')
                {
                    pre = pch;
                    j++;
                }
                else
                {
                    pre = pch;
                    i++;
                    j++;
                }
            }
            else
            {
                if (pre >= 'a' && pre <= 'z')
                {
                    if (isMatch(s.substring(i), p.substring(j+1)))
                    {
                        return true;
                    }
                    k = i;
                    while (k < n && pre == s.charAt(k))
                    {
                        if (isMatch(s.substring(k+1), p.substring(j+1)))
                        {
                            return true;
                        }
                        k++;
                    }
                    return false;
                }
                else if (pre == '*')
                {
                    return false;
                }
                else if (pre == 0)
                {
                    return false;
                }
                else
                {
                    if (isMatch(s.substring(i), p.substring(j+1)))
                    {
                        return true;
                    }
                    k = i;
                    while (k < n)
                    {
                        if (isMatch(s.substring(k+1), p.substring(j+1)))
                        {
                            return true;
                        }
                        k++;
                    }
                    return false;
                }
            }
        }
        if (i < n && j == m)
        {
            return false;
        }
        else if (i == n && j == m)
        {
            return true;
        }
        else if (i == n && j < m)
        {
            return isMatch("", p.substring(j));
        }
        return false;
    }
}

猜你喜欢

转载自blog.csdn.net/da_kao_la/article/details/88083555
今日推荐