Regular Expression Matching,regex,正则表达式匹配,利用动态规划

//动态规划
public class Regex {
    public boolean isMatch(String s, String p) {
        // p长度为0,边界条件。
        if (p.length() == 0) {
            return s.length() == 0;
        }

        // p长度为1,边界条件。
        if (p.length() == 1) {

            // s长度为0
            if (s.length() < 1) {
                return false;
            }
            //首元素匹配有两种情况
            // 如果p为.则s第一个元素和p一定匹配,如果p的第一个元素和s的第一元素相同,也一定匹配。
            else if ((p.charAt(0) != s.charAt(0)) && (p.charAt(0) != ‘.‘)) {
                return false;
            }

            // 否则除了第一个匹配的元素外,比较其他的元素,动态规划的思想。
            else {
                return isMatch(s.substring(1), p.substring(1));
            }
        }

        // p的第二个元素不是*,*代表0个或多个前面的元素
        if (p.charAt(1) != ‘*‘)
        {
            if (s.length() < 1)
            {
                return false;
            }
            else if ((p.charAt(0) != s.charAt(0)) && (p.charAt(0) != ‘.‘))
            {
                return false;
            }
            else
            {
                return isMatch(s.substring(1), p.substring(1));
            }
        }

        else  //p的第二个元素是*
        {
            //*代表0个前面的元素
            if (isMatch(s, p.substring(2)))
            {
                return true;
            }

            //*代表一个或多个前面的元素
            int i = 0;
            while (i<s.length() && (s.charAt(i)==p.charAt(0) || p.charAt(0)==‘.‘))
            {
                if (isMatch(s.substring(i + 1), p.substring(2)))
                {
                    return true;
                }
                i++;
            }
            return false;
        }
    }
    public static void main(String[] args)
    {
        Regex2 reg2 = new Regex2();
        System.out.println(reg2.isMatch("aaba", "ab*a*c*a"));
    }
}
时间: 2025-01-16 02:51:33

Regular Expression Matching,regex,正则表达式匹配,利用动态规划的相关文章

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

翻译 实现支持"."和"*"的正则表达式匹配. "." 匹配支持单个字符 "*" 匹配零个或多个前面的元素 匹配应该覆盖到整个输入的字符串(而不是局部的). 该函数的原型应该是: bool isMatch(const char * s, const char * p) 示例: isMatch("aa","a") → false isMatch("aa","a

Leetcode 10 regular expression matching (正则表达式匹配) (动态规划)

Leetcode 10 问题描述 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 entir

[LeetCode] Regular Expression Matching 正则表达式匹配

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

[LeetCode]10. Regular Expression Matching正则表达式匹配

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 (no

10. Regular Expression Matching字符串.*匹配

[抄题]: 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 str

LeetCode10 Regular Expression Matching

题意: 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 shoul

【leetcode】Regular Expression Matching (hard) ★

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

LeetCode (10): Regular Expression Matching [HARD]

https://leetcode.com/problems/regular-expression-matching/ [描述] 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 ent

LeetCode 10. Regular Expression Matching

https://leetcode.com/problems/regular-expression-matching/description/ Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover