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 == '?'
}
相关推荐
过期动态3 小时前
【LeetCode 热题 100】移动零
java·数据结构·算法·leetcode·职场和发展·rabbitmq
菜菜的顾清寒5 小时前
力扣HOT100(32)二叉树的中序遍历
数据结构·算法·leetcode
csdn_aspnet6 小时前
java 算法 LeetCode 编号 70 - 爬楼梯
java·开发语言·算法·leetcode
_日拱一卒7 小时前
LeetCode:200岛屿数量
算法·leetcode·职场和发展
圣保罗的大教堂8 小时前
leetcode 153. 寻找旋转排序数组中的最小值 中等
leetcode
csdn_aspnet8 小时前
C语言 算法 LeetCode 编号 70 - 爬楼梯
c语言·开发语言·算法·leetcode
菜菜的顾清寒8 小时前
力扣HOT100(31)K 个一组翻转链表
算法·leetcode·链表
x_xbx8 小时前
LeetCode:101. 对称二叉树
算法·leetcode·职场和发展
风筝在晴天搁浅10 小时前
阿里 LeetCode 1189.“气球“的最大数量
算法·leetcode
木井巳10 小时前
【DFS解决floodfill算法】图像渲染
java·算法·leetcode·深度优先