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 == '?'
}
相关推荐
YSRM8 分钟前
Leetcode+Java+图论+最小生成树&拓扑排序
java·leetcode·图论
YSRM9 分钟前
Leetcode+Java+图论+并查集
算法·leetcode·图论
小白杨树树36 分钟前
【C++】力扣hot100错误总结
c++·leetcode·c#
吃着火锅x唱着歌3 小时前
LeetCode 668.乘法表中第k小的数
算法·leetcode·职场和发展
十八岁讨厌编程5 小时前
【算法训练营 · 补充】LeetCode Hot100(上)
算法·leetcode
浮灯Foden5 小时前
算法-每日一题(DAY18)多数元素
开发语言·数据结构·c++·算法·leetcode·面试
小欣加油5 小时前
leetcode 844 比较含退格的字符串
算法·leetcode·职场和发展
如竟没有火炬6 小时前
全排列——交换的思想
开发语言·数据结构·python·算法·leetcode·深度优先
九江Mgx7 小时前
使用 Go + govcl 实现 Windows 资源管理器快捷方式管理器
windows·golang·govcl