LeetCode 44 Wildcard Matching(字符串匹配问题)

题目链接:https://leetcode.com/problems/wildcard-matching/?tab=Description

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

字符串匹配问题。

如上所示:其中 ‘ ?’ 可以匹配任何一个单字符    ’ * ‘ 可以匹配任意长度的字符包括空字符串

给定字符串s,和字符串p。判断按照以上规则,字符串p是否能够匹配字符串s

参考代码:

package leetcode_50;

/***
 *
 * @author pengfei_zheng
 * 字符串匹配问题
 */
public class Solution44 {
    public boolean isMatch(String s, String p) {
        int sp = 0, pp = 0, match = 0, starIdx = -1;
        while (sp < s.length()){
            if (pp < p.length()  && (p.charAt(pp) == ‘?‘
                    || s.charAt(sp) == p.charAt(pp))){
                sp++;
                pp++;
            }
            else if (pp < p.length() && p.charAt(pp) == ‘*‘){
                starIdx = pp;
                match = sp;
                pp++;
            }
            else if (starIdx != -1){
                pp = starIdx + 1;
                match++;
                sp = match;
            }
            else return false;
        }
        while (pp < p.length() && p.charAt(pp) == ‘*‘)
            pp++;

        return pp == p.length();
    }
}
时间: 2024-10-12 12:57:13

LeetCode 44 Wildcard Matching(字符串匹配问题)的相关文章

第八周 Leetcode 44. Wildcard Matching 水题 (HARD)

Leetcode 44 实现一种类似正则表达式的字符串匹配功能. 复杂度要求不高, 调代码稍微费点劲.. 好像跟贪心也不太沾边, 总之 *把待匹配串分成若干个子串, 每一个子串尽量在模式串中靠前的部分匹配完成就算贪心了吧.. class Solution { public: bool match(string &s,string &p,int l2,int r2,int l1) { if(l2==r2)return true; if(l1+r2-l2-1>=s.length())re

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 字符串匹配,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

(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 44 Wildcard Matching

problem:https://leetcode.com/problems/wildcard-matching/ 用记忆化搜索做的,不容易出错: class Solution { public: vector<vector<int>> dp; bool isMatch(const string& s, const string& p, int i, int j) { if(i >= s.size() && j >= p.size()) r

44. Wildcard Matching(js)

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

【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