[LeetCode][JavaScript]Wildcard Matching

Wildcard Matching

Implement wildcard pattern matching with support for ‘?‘ and ‘*‘.

‘?‘ Matches any single character.
‘*‘ Matches any sequence of characters (including the empty sequence).

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", "*") → true
isMatch("aa", "a*") → true
isMatch("ab", "?*") → true
isMatch("aab", "c*a*b") → false

https://leetcode.com/problems/wildcard-matching/



一开始用歪门邪道做果然挂了,合并所有的*,用.*代替*,用.代替?,加开始符^和结束符$,直接用正则表达式去匹配,妥妥的TLE。

贪心+回溯

回溯:当遇到*的时候,p指针往后走一步,如果还是*则继续走,直到不是*为止,这时s不走与p匹配(*匹配的是空)。

如果不匹配,回溯回去,s指针走一步与当前的p匹配(*匹配了一个字符),以此类推。

贪心:当*之后的一个字符与s当前的字符匹配的时候,就认为这之前的字符都匹配好了,之后遇到*不需要回溯到已经匹配好的*了(只需要回溯到最近的*)。

基于这点我们需要记下最近的*的位置,从这里开始回溯就好了。

 1 /**
 2  * @param {string} s
 3  * @param {string} p
 4  * @return {boolean}
 5  */
 6 var isMatch = function(s, p) {
 7     var starS = null, starP = null;
 8     var i = 0, j = 0;
 9     while(s[i]){
10         if(s[i] === p [j] || p[j] === ‘?‘){
11             i++; j++;
12             continue;
13         }
14         if(p[j] === ‘*‘){
15             while(p[j + 1] && p[j + 1] === ‘*‘){
16                 j++;
17             }
18             starS = i; starP = j; j++;
19             continue;
20         }
21         if(starS !== null){
22             i = starS + 1; j = starP + 1; starS++;
23             continue;
24         }
25         return false;
26     }
27     while(p[j] === ‘*‘){
28         j++;
29     }
30
31     return !p[j];
32 };
时间: 2024-08-05 06:40:38

[LeetCode][JavaScript]Wildcard Matching的相关文章

【LeetCode】Wildcard Matching 串匹配 动态规划

题目:Wildcard Matching <span style="font-size:18px;">/*LeetCode WildCard matching * 题目:给定一个目标串和一个匹配串,判定是否能够匹配 * 匹配串中的定义:字符---->字符,*---->0个.1个或者多个字符,?------>对应任意一个字符 * 思路:动态规划:*:dp[i][j] = dp[i][j-1] || dp[i-1][j] * ? || s[i] == p[i]

【leetcode】Wildcard Matching

Wildcard Matching Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. '*' Matches any sequence of characters (including the empty sequence). The matching should cover the entire input string (not partia

LeetCode 044 Wildcard Matching

题目要求:Wildcard Matching Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. '*' Matches any sequence of characters (including the empty sequence). The matching should cover the entire input string (not p

leetCode 44.Wildcard Matching (通配符匹配) 解题思路和方法

Wildcard Matching '?' Matches any single character. '*' Matches any sequence of characters (including the empty sequence). The matching should cover the entire input string (not partial). The function prototype should be: bool isMatch(const char *s,

【leetcode】Wildcard Matching(hard) ★ 大神太牛了

Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. '*' Matches any sequence of characters (including the empty sequence). The matching should cover the entire input string (not partial). The function p

Java for LeetCode 044 Wildcard Matching

Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. '*' Matches any sequence of characters (including the empty sequence). The matching should cover the entire input string (not partial). The function p

(Java) LeetCode 44. Wildcard Matching —— 通配符匹配

Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. '*' Matches any sequence of characters (including the empty sequence). The matching should cover the enti

19.2.4 [LeetCode 44] Wildcard Matching

Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. '*' Matches any sequence of characters (including the empty sequence). The matching should cover the enti

LeetCode题解-----Wildcard Matching

题目描述: '?' Matches any single character. '*' Matches any sequence of characters (including the empty sequence). The matching should cover the entire input string (not partial). The function prototype should be: bool isMatch(const char *s, const char *