Golang | Leetcode Golang题解之第44题通配符匹配

题目:

题解:

Go 复制代码
func isMatch(s string, p string) bool {
    for len(s) > 0 && len(p) > 0 && p[len(p)-1] != '*' {
        if charMatch(s[len(s)-1], p[len(p)-1]) {
            s = s[:len(s)-1]
            p = p[:len(p)-1]
        } else {
            return false
        }
    }
    if len(p) == 0 {
        return len(s) == 0
    }
    sIndex, pIndex := 0, 0
    sRecord, pRecord := -1, -1
    for sIndex < len(s) && pRecord < len(p) {
        if p[pIndex] == '*' {
            pIndex++
            sRecord, pRecord = sIndex, pIndex
        } else if charMatch(s[sIndex], p[pIndex]) {
            sIndex++
            pIndex++
        } else if sRecord != -1 && sRecord + 1 < len(s) {
            sRecord++
            sIndex, pIndex = sRecord, pRecord
        } else {
            return false
        }
    }
    return allStars(p, pIndex, len(p))
}

func allStars(str string, left, right int) bool {
    for i := left; i < right; i++ {
        if str[i] != '*' {
            return false
        }
    }
    return true
}

func charMatch(u, v byte) bool {
    return u == v || v == '?'
}
相关推荐
yaoh.wang10 小时前
力扣(LeetCode) 100: 相同的树 - 解法思路
python·程序人生·算法·leetcode·面试·职场和发展·跳槽
SadSunset10 小时前
力扣题目142. 环形链表 II的解法分享,附图解
算法·leetcode·链表
iAkuya11 小时前
(leetcode)力扣100 19螺旋矩阵(方向数组/边界把控)
算法·leetcode·矩阵
爱编程的小吴11 小时前
【力扣练习题】热题100道【哈希】 最长连续序列
算法·leetcode·职场和发展
Rinai_R12 小时前
Go 的调度模型
开发语言·后端·golang
bybitq12 小时前
Leetcode-3780-Python
python·算法·leetcode
如何原谅奋力过但无声12 小时前
【力扣-Python-75】颜色分类(middle)
python·算法·leetcode
玖剹12 小时前
哈希表相关题目
数据结构·c++·算法·leetcode·哈希算法·散列表
练习时长一年14 小时前
LeetCode热题100(最小栈)
java·算法·leetcode
Tisfy14 小时前
LeetCode 955.删列造序 II:模拟(O(mn)) + 提前退出
算法·leetcode·字符串·题解·遍历