【算法】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 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 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 = "ab"
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

通配符匹配,s为小写字符串,p为包含小写字母、*、?的通配符,判断p是否匹配s成功:

  1. ?匹配一个任意字符
  2. * 匹配 0 至无限多个任意字符
  3. s 和 p 均为小写字符串

解题思路

?和字母是普通的单个字符匹配,p[j] == ? || p[j] == s[i](i、j分比为s、p当前字符的索引)则匹配成功,否则匹配失败。
* 由于可以匹配任意多个任意字符,其实就可以作为当单个字符匹配失败时,续命的作用。

  1. 如果p[j] == ? || p[j] == s[i]则匹配成功,i、j 后移一位。
  2. 如果p[j] == *,starJ 记录 * 位置 j ,waitingMatchI 记录待 * 匹配字符位置 i ,j 后移一位。
  3. 如果当前字符匹配失败,且 starJ 记录了 * 的位置(starJ != -1),则将 waitingMatchI 记录的字符用 * 匹配,waitingMatchI 后移一位,将 j 拉回到 starJ 的后一位,将 i 拉回到 waitingMatchI。
  4. 如果当前字符匹配失败,且 starJ 没有记录 * 的位置,则匹配完全失败,返回 false
  5. s 遍历匹配结束后,遍历 p 剩余字符,若为 * 则 j 后移一位,只有剩余字符全为 * 才可匹配空字符成功,即 j 移动到了 p 末尾
  6. 判断 j 是否移动到了 p 末尾,是,则匹配成功,否,则匹配失败

代码实现

func wildcardMatchingChecker1(_ s: String, p: String) -> Bool {
        //将字符串转为字符数组
        let sArr = s.map({$0})
        let pArr = p.map({$0})
        //声明*和?
        let star = Character("*")
        let qMark = Character("?")
        //i记录s中字符位置,j记录p中字符位置,waitingMatchI记录*匹配后s中待匹配字符位置,starJ记录*在p中位置
        var i = 0
        var j = 0
        var waitingMatchI = -1
        var starJ = -1
        //开始循环
        while i < sArr.count {
            //如果j有效,且s、p对应字符相同,或者p对应字符为?,则匹配成功,i、j均+1
            if (j < pArr.count && (sArr[i] == pArr[j] || pArr[j] == qMark)) {
                i += 1
                j += 1
            //匹配失败,如果j有效,并且p对应字符为*,starJ记录*位置j,j+=1,waitingMatchI记录s中待*匹配字符位置i
            }else if(j < pArr.count && pArr[j] == star){
                starJ = j
                j += 1
                waitingMatchI = i
            //匹配失败且p对应字符不为*,若starJ != -1,表明之前有*匹配
            //将j重置回starJ + 1,waitingMatchI字符视为被*匹配,将waitingMatchI+1,i重置为waitingMatchI
            }else if (starJ != -1){
                j = starJ + 1
                waitingMatchI += 1
                i = waitingMatchI
            //匹配失败,p对应字符不为*,之前没有*匹配,则匹配彻底失败
            }else{
                return false
            }
        }
        //s匹配结束后,如果p仍有剩余字符,遍历是否均为*
        while (j < pArr.count && pArr[j] == star) {
            j += 1
        }
        //若j走到了p的最后,则匹配成功,否则匹配失败
        return j == pArr.count
    }

代码地址:https://github.com/sinianshou/EGSwiftLearning

你可能感兴趣的:(【算法】Wildcard Matching通配符匹配)