Golang | Leetcode Golang题解之第76题最小覆盖子串

题目:

题解:

Go 复制代码
func minWindow(s string, t string) string {
    ori, cnt := map[byte]int{}, map[byte]int{}
    for i := 0; i < len(t); i++ {
        ori[t[i]]++
    }

    sLen := len(s)
    len := math.MaxInt32
    ansL, ansR := -1, -1

    check := func() bool {
        for k, v := range ori {
            if cnt[k] < v {
                return false
            }
        }
        return true
    }
    for l, r := 0, 0; r < sLen; r++ {
        if r < sLen && ori[s[r]] > 0 {
            cnt[s[r]]++
        }
        for check() && l <= r {
            if (r - l + 1 < len) {
                len = r - l + 1
                ansL, ansR = l, l + len
            }
            if _, ok := ori[s[l]]; ok {
                cnt[s[l]] -= 1
            }
            l++
        }
    }
    if ansL == -1 {
        return ""
    }
    return s[ansL:ansR]
}
相关推荐
快去睡觉~5 小时前
力扣73:矩阵置零
算法·leetcode·矩阵
岁忧5 小时前
(nice!!!)(LeetCode 每日一题) 679. 24 点游戏 (深度优先搜索)
java·c++·leetcode·游戏·go·深度优先
小欣加油5 小时前
leetcode 3 无重复字符的最长子串
c++·算法·leetcode
元亓亓亓10 小时前
LeetCode热题100--101. 对称二叉树--简单
算法·leetcode·职场和发展
1白天的黑夜113 小时前
链表-24.两两交换链表中的结点-力扣(LeetCode)
数据结构·leetcode·链表
快去睡觉~17 小时前
力扣48:旋转矩阵
算法·leetcode·矩阵
卡洛斯(编程版18 小时前
(1) 哈希表全思路-20天刷完Leetcode Hot 100计划
python·算法·leetcode
MrZhangBaby20 小时前
SQL-leetcode—3374. 首字母大写 II
linux·sql·leetcode
bianshaopeng20 小时前
ubuntu go 环境变量配置
开发语言·ubuntu·golang
元清加油20 小时前
【Goland】:协程和通道
服务器·开发语言·后端·网络协议·golang