1. 程式人生 > >String、動態規劃——wildcard-matching 萬用字元匹配

String、動態規劃——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

遞迴程式碼執行超時,但在eclipse上自己測試了幾個用例能通過:

public class Solution {
    public boolean isMatch(String s, String p) {
        if(s == null||p == null)
            return false;
        
         return matchCore(s,0,p,0);
    }
    public boolean matchCore(String s,int sindex,String p,int pindex)
        {
        //邊界條件
        if(sindex == s.length()&&pindex == p.length())//都到達末尾時返回true
            return true;
        
        else if(pindex == p.length())//p到達末尾而s沒有,則返回false;
            return false;
            
            else if(sindex ==s.length())//s到達末尾,p餘下的全部為‘*’返回true,否則返回false;
            {
            if(p.charAt(pindex) == '*')
                {
                return matchCore(s,sindex,p,pindex+1);
            }else
                {
                return false;
            }
        }
        
        //一般條件
        if(s.charAt(sindex) == p.charAt(pindex)||p.charAt(pindex) == '?')
         {
            return matchCore(s,sindex+1,p,pindex+1);
        }
        else if(p.charAt(pindex) == '*')//當前字元為'*'時的情況;
            {
            return matchCore(s,sindex+1,p,pindex+1)||
                   matchCore(s,sindex,p,pindex+1)||
                   matchCore(s,sindex+1,p,pindex);
        }
        else
            {
            return false;
        }
    }
}

動態規劃:

用空間換時間。執行時間130ms,佔用記憶體5000K。

public class Solution {
    public boolean isMatch(String s, String p) {
        if(s == null|| p == null)
            return false;
        
        int m=s.length()+1;
        int n=p.length()+1;
        
        //flag[i][j]表示s.length() == i,p.length()== j時匹配狀況;
        boolean [][]flag=new boolean [m][n];
               
        //邊界情況:
        
        //s.length和p.length為0時 返回true;
        flag[0][0]=true;
        //p.length為0,s.length不為0時返回false;
        for(int i=1;i<m;i++)
            {
            flag[i][0]=false;
        }
        //s.length為0,p.length不為0,只有當p其餘全為'*'時,返回true,否則返回false;
        for(int j=1;j<n;j++)
            {
            if(p.charAt(j-1) == '*')
                {
                flag[0][j]=flag[0][j-1];
            }else
                {
                flag[0][j]=false;
            }
        }
        
        //一般情況:
        
        for(int i=1;i<m;i++)
            {
            for(int j=1;j<n;j++)
                {
                if(s.charAt(i-1) == p.charAt(j-1)||p.charAt(j-1) == '?')
                    {
                    flag[i][j]=flag[i-1][j-1];
                }
                else if(p.charAt(j-1) == '*')
                    {
                    flag[i][j]=flag[i-1][j]||flag[i-1][j-1]||flag[i][j-1];
                }
                else
                    {
                    flag[i][j]=false;
                }
            }
        }
        
        return flag[m-1][n-1];
    }
}


相關推薦

String動態規劃——wildcard-matching 字元匹配

Implement wildcard pattern matching with support for'?'and'*'. '?' Matches any single character. '*' Matches any sequence of characters

[leetcode] Wildcard Matching 字元匹配

也是《劍指offer》中的題目 問題描述: 判斷兩個可能包含萬用字元“?”和“*”的字串是否匹配。匹配規則如下: '?' 可以匹配任何單個字元。 '*' 可以匹配任意字串(包括空字串)。 兩個串完全匹配才算匹配成功。 介面如下:其中s為待匹配串(不含'?'和'*'

D-動態規劃比遞迴快-LeetCode44-字元匹配

題目 給定一個字串 (s) 和一個字元模式 (p) ,實現一個支援 '?' 和 '*' 的萬用字元匹配。 '?' 可以匹配任何單個字元。 '*' 可以匹配任意字串(包括空字串)。 兩個字串完全匹配才算匹配成功。 說明: s 可能為空,且只包含從 a-z 的小寫字母。 p 可能為空,且只包含

leetcode 44 ---- 動態規劃(困難) :字元匹配(java)

1.  問題:給定一個字串 (s) 和一個字元模式 (p) ,實現一個支援 '?' 和 '*' 的萬用字元匹配。 '?' 可以匹配任何單個字元。 '*' 可以匹配任意字串(包括空字串)。 兩個字串完全匹配才算匹配成功。 說明: s 可能為空,且只包含從 a-z 的小

LeetCode-44.字元匹配(考察點:動態規劃

給定一個字串 (s) 和一個字元模式 (p) ,實現一個支援 '?' 和 '*' 的萬用字元匹配。 '?' 可以匹配任何單個字元。 '*' 可以匹配任意字串(包括空字串)。 兩個字串完全匹配才算匹配成功。 說明: s 可能為空,且只包含從 a-z 的小寫字母。 p

String動態規劃——正則表示式匹配

public class Solution { public boolean isMatch(String s, String p) { if(s == null||p == null) return false; boolean [][]fla

Wildcard Matching字元匹配

Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. '*' Matches any sequence of charact

字元匹配字串 Wildcard Matching

問題:實現支援?和*兩個萬用字元的字串匹配函式。 Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. '*' Matches

單域名版多域名版和字元版SSL證書之間的區別

本博主轉載前必先親自考證,深惡痛絕百度CP之流,有問題請與我聯絡。 單域名版SSL證書 顧名思義,只保護一個域名,這些域名形如 www.yuming.com;pay.domain.net;shop.store.cn 等; 值得注意的有兩點: 1、當您位 www 字首的域名申請證書的時

linux|批量建立檔案資料夾或刪除——字元

前言: 在關於檔案操作的部分,我們之前學到的一些有趣的命令。它有一個問題就是一次只能在一個檔案上執行,效率不高。那麼萬用字元就派上用場了!!! 萬用字元是什麼: 萬用字

DMI動態方法呼叫 和 字元

動態方法呼叫DMI <action name="user" class="com.action.UserAction">          <result>/user_add_success.jsp</result> </ac

Struts2-Action接受引數方式method屬性使用及字元的配置

一、Action接受引數的方式 1、屬性方式接收 首先編寫一個用於上傳引數的頁面 action配置 成功跳轉的頁面 action類程式碼 public cla

Leetcode 44:字元匹配(超詳細的解法!!!)

給定一個字串 (s) 和一個字元模式 (p) ,實現一個支援 '?' 和 '*' 的萬用字元匹配。 '?' 可以匹配任何單個字元。 '*' 可以匹配任意字串(包括空字串)。 兩個字串完全匹配才算匹配成功。 說明: s 可能為空,且只包含從 a-z 的小寫字母。

[Luogu P3167] [BZOJ 3507] [CQOI2014]字元匹配

洛谷傳送門 BZOJ傳送門 題目描述 幾乎所有作業系統的命令列介面(CLI)中都支援檔名的萬用字元匹配以方便使用者。最常見的萬用字元有兩個,一個是星號(*),可以匹配 0

字元匹配

給定一個字串 (s) 和一個字元模式 § ,實現一個支援 ‘?’ 和 ‘*’ 的萬用字元匹配。 ‘?’ 可以匹配任何單個字元。 ‘*’ 可以匹配任意字串(包括空字串)。 兩個字串完全匹配才算匹配成功。 說明: s 可能為空,且只包含從 a-z 的小寫字母。 p 可能為空,且只包含

leetcode 44. 字元匹配

給定一個字串 (s) 和一個字元模式 (p) ,實現一個支援 '?' 和 '*' 的萬用字元匹配。 '?' 可以匹配任何單個字元。 '*' 可以匹配任意字串(包括空字串)。 兩個字串完全匹配才算匹

【leetCode】44_字元匹配

dp: class Solution { public: bool isMatch(string s, string p) { //tag 用於dp,tag[i][j]代表s的子串s[0,i-1] 可以匹配 p的子串p[0,j-1]。

【LeetCode】44. 字元匹配

題目描述 思路(與10. 正則表示式匹配類似) dp[ i ][ j ] 表示 s 的前 i 個字元與 p 的前 j 個字元的匹配結果。 先寫邊界情況,再寫 dp 方程,若 p[ j - 1] 為 * ,dp[ i ][ j ] = dp[ i - 1][ j

字元匹配_講解和python3實現

題目描述 給定一個字串 (s) 和一個字元模式 § ,實現一個支援 ‘?’ 和 ‘*’ 的萬用字元匹配。 ‘?’ 可以匹配任何單個字元。 ‘*’ 可以匹配任意字串(包括空字串)。 兩個字串完全匹配才算匹配成功。 說明: s 可能為空,且只包含從 a-z 的小寫字母。 p 可能

LeetCode 10 & 44 正則表示式匹配 & 字元匹配 字串匹配問題

10 正則表示式匹配 給定一個字串 (s) 和一個字元模式 (p)。實現支援 ‘.’ 和 ‘*’ 的正則表示式匹配。 ‘.’ 匹配任意單個字元。 ‘*’ 匹配零個或多個前面的元素。 匹配應該覆蓋整個字串 (s) ,而不是部分字串。 說明: s 可能為