zoukankan      html  css  js  c++  java
  • 44. Wildcard Matching

    description:

    匹配字符串,具体看例子
    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 entire input string (not partial).
    Note:

    s could be empty and contains only lowercase letters a-z.
    p could be empty and contains only lowercase letters a-z, and characters like ? or *.
    

    Example:

    Example 1:
    
    Input:
    s = "aa"
    p = "a"
    Output: false
    Explanation: "a" does not match the entire string "aa".
    
    Example 2:
    
    Input:
    s = "aa"
    p = "*"
    Output: true
    Explanation: '*' matches any sequence.
    
    Example 3:
    
    Input:
    s = "cb"
    p = "?a"
    Output: false
    Explanation: '?' matches 'c', but the second letter is 'a', which does not match 'b'.
    
    Example 4:
    
    Input:
    s = "adceb"
    p = "*a*b"
    Output: true
    Explanation: The first '*' matches the empty sequence, while the second '*' matches the substring "dce".
    
    Example 5:
    
    Input:
    s = "acdcb"
    p = "a*c?b"
    Output: false
    
    

    answer:

    class Solution {
    public:
        bool isMatch(string s, string p) {
            int i = 0, j = 0, iStar = -1, jStar = -1;
            while (i < s.size()) {
                if (s[i] == p[j] || p[j] == '?') {
                    ++i; ++j;
                } else if (p[j] == '*') {
                    iStar = i;
                    jStar = j++; // 这里是先标记位置,然后在挪动 j index
                } else if (iStar >= 0) {
                    i = ++iStar; //这里一定是++iStar,而不是 iStar+1,因为不但要挪,还要改变 iStar
                    j = jStar + 1;
                } else return false;
            }
            while (j < p.size() && p[j] == '*') ++j;
            return j == p.size();
        }
    };
    

    relative point get√:

    hint :

    两个都从头开始走,一样就继续,不一样就三种可能,一种是‘?’,继续;一种是'*',标记完位置之后上边字符串不变,下边走一步(先假设'*'是空,然后往后走,走到不行了再用这个开挂神器);最后一种就是真的不一样,这个时候就想起来看之前有没有'*',有的话,就用'*'去匹配上边的,一直匹配到上边出现某个字符和下边‘*’右边那个字符一样,然后继续开始之前正常的运作。最后上边的字符都匹配完了,如果下面字符还剩下了,还不是‘*’,那也是失败。

  • 相关阅读:
    代理类和装饰类的区别
    spring mvc 处理映射的几种方式
    如何深入浅出的理解跳转方式:重定向和请求转发
    springMVC拦截配置
    ※版本管理※=>☆SVN工具=>※解决地域麻烦※№→搭建自己的网络SVN (SourceForge 免费) [转]
    权力社会? 金钱社会? 透过现象看本质-让权力和金钱的力量沿着制度的河道流淌,才是社会稳定的基石
    自己封装的CMusic类 【转】
    VC++中MCI播放音频文件 【转】
    DevExpress.XtraGrid 【转】
    C# Process.Start()方法详解 [转]
  • 原文地址:https://www.cnblogs.com/forPrometheus-jun/p/11193209.html
Copyright © 2011-2022 走看看