`

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:
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

[balabala] dp[i][j] 表示p的前i个字符能否匹配s的前j个字符。分两大类情况讨论:
1)p的当前字符 != '*': dp[i][j] = dp[i - 1][j - 1] && (currP == currS || currP == '.');
2) p的当前字符 == '*': dp[i][j] 为true的条件是星号要么匹配0次,要么匹配1次,要么匹配多次。匹配多次时根据p的前一个字符分为两种情况:前字符是'.'对s的当前字符无限制, 前字符非 '.'时要求s的当前字符和p的前字符相同
此外,注意到dp[i][0] = dp[i - 2][0] && currp == '*'

public boolean isMatch(String s, String p) {
        if (s == null || p == null)
            return false;
        int lengthS = s.length();
        int lengthP = p.length();
        boolean[][] dp = new boolean[lengthP + 1][lengthS + 1];
        dp[0][0] = true;
        for (int i = 1; i <= lengthP ; i++) {
            char currP = p.charAt(i - 1);
            if (i >= 2) {
                dp[i][0] = dp[i - 2][0] && currP == '*'; 
            } else {
                dp[i][0] = false;
            }
            for (int j = 1; j <= lengthS; j++) {
                if (currP != '*') {
                    dp[i][j] = dp[i - 1][j - 1] && (currP == s.charAt(j - 1) || currP == '.');
                } else if(i >= 2) {
                    char lastP = p.charAt(i - 2);
                    dp[i][j] = dp[i - 2][j] || dp[i - 1][j] || (dp[i][j - 1] && (lastP == '.' || lastP == s.charAt(j - 1)));
                }
            }
        }
        return dp[lengthP][lengthS];
}

分享到:
评论

相关推荐

    js-leetcode题解之10-regular-expression-matching.js

    js js_leetcode题解之10-regular-expression-matching.js

    C语言-leetcode题解之10-regular-expression-matching.c

    c语言入门 C语言_leetcode题解之10-regular-expression-matching.c

    c语言-leetcode 0010-regular-expression-matching.zip

    c c语言_leetcode 0010_regular_expression_matching.zip

    _leetcode-python.pdf

    - Regular Expression Matching: 给定一个输入字符串和一个模式,实现支持'.'和'*'的正则表达式匹配。 - Container With Most Water: 给定一个数组,其中每个元素代表一个宽度为1的柱子高度,要求找出两个柱子,使得...

    分割数组求最大差值leetcode-Leetcode-Road:LeetCode刷题记录

    分割数组求最大差值leetcode LeetCode 学习之路 记录自己完成LeetCode的代码和结果。...Expression Matching 25.3% 困难 11 Container With Most Water 59.3% 中等 12 Integer to Roman 61.8% 中等 13 Roman to In

    lrucacheleetcode-leetcode-1:leetcode-1

    lru cache leetcode Leetcode ...Expression Matching 动态规划,列出转换方程即可,注意初值 记T[i][j] = 是否S[0:i]和P[0:j]匹配 再分类讨论,其中pattern *分为0, 1, more三种类型 0: i不变, j+1

    LeetCode-in-Golang:使用 Golang 解答 LeetCode

    35%2☆ ☆27%3☆ ☆24%4☆ ☆ ☆21%5☆ ☆25%6☆ ☆26%7☆24%8☆ ☆13%9Palindrome Number☆35%10Regular Expression Matching☆ ☆ ☆24%:red_heart:11Container With Most Water☆ ☆36%12Integer to R

    leetcode写题闪退-LeetCode:leetcodeOJ

    leetcode写题闪退 #*的多少代表此题的有意思程度 有几题第一次写的时候思绪比较混乱: *****Regular Expression Matching 2014.10.29 对于Find Minimum in Rotated Sorted Array II 和 Find Minimum in Rotated ...

    LeetCode题解 - Java语言实现-181页.pdf

    8. Regular Expression Matching in Java 正则表达式匹配是一个字符串问题,要求使用正则表达式来匹配字符串。可以使用Java的Pattern和Matcher类来解决该问题。 9. Merge Intervals 区间合并是一个数组问题,要求...

    leetcode中国-leetcode:leetcode刷题

    Expression Matching 递归匹配 wildcard matching 动态规划 longest common prefix , 简单 valid number, hard, 用有限自动机 integer to roman ,easy , 模拟 roman to integer ,easy , 模拟 count and say , easy ,...

    leetcode338-LeetCode:LeetCode刷题总结

    LeetCode刷题总结 1.Two Sum 2.Add Two Numbers 3.Longest Substring Without Repeating Characters 4.Median of Two Sorted Arrays 5.Longest Palindromic Substring (Manacher算法待完成) 6.ZigZag Conversion 7....

    lrucacheleetcode-leetcode:leetcodesolutionsinC++微信公众号:曲奇泡芙(互联网&智能汽车技术)

    lru cache leetcode leetcode leetcode solutions in C++ 微信公众号:曲奇泡芙 (互联网&车联网技术) ..../0010-regular-expression-matching.cpp ./0011-container-with-most-water.cpp ./0012-int

    lrucacheleetcode-luoleet:LeetcodesolutionsbyXinhangLuoandQinghaoDai

    https://leetcode.com/problems/regular-expression-matching/ Regular Expression Matching 100 https://leetcode.com/problems/same-tree/ Same Tree 101 https://leetcode.com/problems/symmetric-tree/ ...

    leetcode答案-leetcode:leetcode

    leetcode 答案 leet code 这是我的leetcode答案,语言主要是用python。很多算法不是最优,甚至很槽糕,有空就会优化 目前完成了: Two Sum Add Two Numbers Longest ...Regular Expression Matching

    leetcode530-algorithm:算法

    Expression Matching 011 Container With Most Water 012 Integer to Roman 013 Roman to Integer 014 Longest Common Prefix 015 3Sum 016 3Sum Closest 017 Letter Combinations of a Phone Number 018 4Sum 020 ...

    leetcode跳跃-LeCode:乐科

    leetcode 跳跃 LeetCode Solved by ...Expression Matching 正则表达式匹配 11. Container With Most Water 盛最多水的容器 12. Integer to Roman 整数转罗马数字 13. Roman to Integer 罗马数字转

    lrucacheleetcode-LeetCode_Note:leetcode个人笔记

    [10_regular-expression-matching.cpp] [100_same-tree.cpp] [1001_sorted-merge-lcci.cpp] [101_对称树.cpp] [102_binary-tree-level-order-traversal.cpp] [103_binary-tree-zigzag-level-order-traversal.cpp] ...

    leetcode分类-leetcode:leetcode刷题

    4. **正则表达式匹配**:“实现一个简单的正则表达式匹配器”(Regular Expression Matching),涉及到递归和动态规划。 5. **字符串编码与解码**:“无重复字符的最长子串”(Longest Substring Without Repeating...

    Python_leetcode.zip

    "regular-expression-matching.py"考察的是字符串匹配和回溯算法。Python的字符串操作和递归可以构建出复杂的正则表达式匹配算法,帮助解决复杂的文本处理问题。 "palindrome-pairs.py"是关于回文串和字符串匹配的...

    lrucacheleetcode-LeetCode:我从LeetCode提交的一些任务

    (/problems/regular-expression-matching/) 24.1% 难253  38.9% 中15 21.6% 中277 寻找名人 (/problems/find-the-celebrity/)  35.4% 中等158 读取 N 个字符给定 Read4 II - 多次调用 (/problems/read-n-...

Global site tag (gtag.js) - Google Analytics