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 == '?'
}
相关推荐
MoonBit月兔1 小时前
插件双更新:LeetCode 刷题支持正式上线,JetBrains IDE 插件持续升级!
ide·算法·leetcode
袁气满满~_~3 小时前
LeetCode:617、合并二叉树
算法·leetcode·二叉树
边跑边掩护3 小时前
LeetCode 820 单词的压缩编码题解
算法·leetcode·职场和发展
Espresso Macchiato3 小时前
Leetcode 3543. Maximum Weighted K-Edge Path
leetcode·leetcode medium·图遍历·leetcode 3543·leetcode双周赛156
Clown954 小时前
go-zero(十八)结合Elasticsearch实现高效数据检索
开发语言·elasticsearch·golang
爱coding的橙子4 小时前
每日算法刷题计划Day7 5.15:leetcode滑动窗口4道题,用时1h
算法·leetcode
阳洞洞5 小时前
leetcode 56. 合并区间
leetcode
小刘不想改BUG5 小时前
LeetCode LCR 015. 找到字符串中所有字母异位词 (Java)
linux·算法·leetcode
Musennn11 小时前
leetcode 15.三数之和 思路分析
算法·leetcode·职场和发展