Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

10. Regular Expression Matching #223

Open
Tcdian opened this issue Jun 20, 2020 · 1 comment
Open

10. Regular Expression Matching #223

Tcdian opened this issue Jun 20, 2020 · 1 comment

Comments

@Tcdian
Copy link
Owner

Tcdian commented Jun 20, 2020

10. Regular Expression Matching

给你一个字符串 s 和一个字符规律 p,请你来实现一个支持 '.''*' 的正则表达式匹配。

'.' 匹配任意单个字符
'*' 匹配零个或多个前面的那一个元素

所谓匹配,是要涵盖 整个 字符串 s的,而不是部分字符串。

Note

  • s 可能为空,且只包含从 a-z 的小写字母。
  • p 可能为空,且只包含从 a-z 的小写字母,以及字符 .*

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

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
@Tcdian
Copy link
Owner Author

Tcdian commented Jun 20, 2020

Solution

  • JavaScript Solution
/**
 * @param {string} s
 * @param {string} p
 * @return {boolean}
 */
var isMatch = function(s, p) {
    const dp = Array.from(new Array(p.length + 1), () => new Array(s.length + 1));
    for(let i = 0; i <= p.length; i++) {
        for (let j = 0; j <= s.length; j++) {
            if (i === 0 && j === 0) {
                dp[i][j] = true;
            } else if (i == 0) {
                dp[i][j] = false;
            } else if (p[i - 1] === '.' || p[i - 1] === s[j - 1]) {
                dp[i][j] = dp[i - 1][j - 1] || false;
            } else if (p[i - 1] === '*') {
                if (dp[i - 2][j]) {
                    dp[i][j] = true;
                    continue;
                }
                if (p[i - 2] === '.' || p[i - 2] === s[j - 1]) {
                    dp[i][j] = dp[i][j - 1] || dp[i - 1][j];
                    continue;
                }
                dp[i][j] = false;
            } else {
                dp[i][j] = false;
            }
        }
    }
    return dp[p.length][s.length];
};
  • TypeScript Solution
function isMatch(s: string, p: string): boolean {
    const dp: boolean[][] = Array.from(new Array(p.length + 1), () => new Array(s.length + 1));
    for(let i = 0; i <= p.length; i++) {
        for (let j = 0; j <= s.length; j++) {
            if (i === 0 && j === 0) {
                dp[i][j] = true;
            } else if (i == 0) {
                dp[i][j] = false;
            } else if (p[i - 1] === '.' || p[i - 1] === s[j - 1]) {
                dp[i][j] = dp[i - 1][j - 1] || false;
            } else if (p[i - 1] === '*') {
                if (dp[i - 2][j]) {
                    dp[i][j] = true;
                    continue;
                }
                if (p[i - 2] === '.' || p[i - 2] === s[j - 1]) {
                    dp[i][j] = dp[i][j - 1] || dp[i - 1][j];
                    continue;
                }
                dp[i][j] = false;
            } else {
                dp[i][j] = false;
            }
        }
    }
    return dp[p.length][s.length];
};

@Tcdian Tcdian removed the Classic label Jul 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant