[Leetcode] Wildcard Matching

*匹配任意多个字符,.匹配单个字符

关键在于*的情况,如果遇到了星号,那么需要枚举所有与星号匹配的情况,包括空串的情况

一、递归解法

 1 boolean isMRecursive(String s,String p){
 2     if(s.compareTo("")==0) return p.compareTo("")==0;
 3     if(p.charAt(0)==‘*‘){
 4         for(int i=0;i<s.length();i++){
 5             if(isM(s.substring(i),p)) return true;
 6         }
 7         return false;
 8     }else{
 9         if(p.charAt(0)==‘.‘||(p.charAt(0)==s.charAt(0))){
10             return isM(s.substring(1),p.substring(1));
11         }else{
12             return false;
13         }
14     }
15 }

二、非递归解法

boolean isMIterate(String s,String p){
    if(s.compareTo("")==0) return p.compareTo("")==0;
    int i=0,j=0;
    int start=-1;
    int ss=0;
    while(i<s.length()&&j<p.length()){
        if(i==s.length()) return j==p.length();
        if(p.charAt(j)==‘*‘){
            start=j;
            ss=i+1;
            i++;
        }else if(p.charAt(j)==‘.‘||p.charAt(j)==s.charAt(i)){
            i++;
            j++;
        }else if(start!=-1){
            i=ss;
            j=start+1;
        }else{
            return false;
        }
    }
}
时间: 2024-08-06 23:53:05

[Leetcode] Wildcard Matching的相关文章

LeetCode: Wildcard Matching [043]

[题目] 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 funct

[LeetCode]wildcard matching通配符实现之贪心法

前天用递归LTE,昨天用动态规划LTE,今天接着搞,改用贪心法.题目再放一次: '?'匹配任意字符,'*'匹配任意长度字符串 Some examples: isMatch("aa","a") → false isMatch("aa","aa") → true isMatch("aaa","aa") → false isMatch("aa", "*"

[LeetCode]wildcard matching通配符实现之动态规划

前天wildcard matching没AC,今天接着搞,改用动态规划.题目再放一次: '?'匹配任意字符,'*'匹配任意长度字符串 Some examples: isMatch("aa","a") → false isMatch("aa","aa") → true isMatch("aaa","aa") → false isMatch("aa", "*&q

[LeetCode]wildcard matching通配符实现TLE

leetcode这道题还挺有意思的,实现通配符,'?'匹配任意字符,'*'匹配任意长度字符串,晚上尝试了一下,题目如下: Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. '*' Matches any sequence of characters (including the empty sequence). The matching shoul

Leetcode: Wildcard Matching. java

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

LeetCode: Wildcard Matching 解题报告

Wildcard MatchingImplement 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)

[LeetCode] Wildcard Matching 字符串匹配,kmp,回溯,dp

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

[LeetCode] 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

[LeetCode]Wildcard Matching 通配符匹配(贪心)

一開始採用递归写.TLE. class Solution { public: bool flag; int n,m; void dfs(int id0,const char *s,int id1,const char *p){ if(flag)return; if(id0>=n){ if(id1>=m)flag=1; else{ int j=0; while(j<m&&p[j]=='*')j++; if(j>=m)flag=1; } return; } else i