[leetcode] 76. 最小覆盖子串

代码

第一次

/**
 * @param {string} s
 * @param {string} t
 * @return {string}
 */
var minWindow = function(s, t) {
    let l = 0
    let r = 0
    let isRight = true
    let maxStr = ''
    let maxNum = 0
    const tMapInitial = new Map()
    const tMap = new Map()
    // 初始化
    for(let i = 0; i < t.length; i++) {
        tMap.set(t[i], 0)
        if(tMapInitial.get(t[i])) {
            tMapInitial.set(t[i], tMapInitial.get(t[i]) + 1)
        }else {
            tMapInitial.set(t[i],1)
        }
    }
    // 遍历
    while(r < s.length) {
        if(isRight) {
            // 右指针移动的情况
            if(tMap.has(s[r])) {
                tMap.set(s[r], tMap.get(s[r]) + 1)
            }
        }else {
            // 左指针移动的情况
            if(tMap.has(s[l])) {
                tMap.set(s[l], tMap.get(s[l]) - 1)
            }
            l++
        }

        // 覆盖t中的字符数量
        let check = 0
        for(let [key, value] of tMap.entries()) {
            if(value >= tMapInitial.get(key) && value > 0) {
                check++
            }
        }
        // 判断是否已完全覆盖t所有字符
        if(check === tMapInitial.size) {
            isRight = false
        }else {
            isRight = true
        }    

        // 更新maxStr
        const locStr = s.substring(l, r+1)
        if(check > maxNum) {
            maxNum = check
            maxStr = locStr
        }else if(check == maxNum && check !== 0) {
            if(maxStr.length > locStr.length || maxStr === '') {
                maxStr = locStr
            }
        }     

        if(isRight) {
            r++
        }
    }
    // 如果没有覆盖t所有字符或maxstr长度小于t
    if(maxNum !== tMapInitial.size || maxStr.length < t.length) {
        return ''
    }

    return maxStr
};

在这里插入图片描述

第二次

/**
 * @param {string} s
 * @param {string} t
 * @return {string}
 */
var minWindow = function(s, t) {
    const need = new Map()
    // 将t放入字典(所需要的字符以及相应的数量)
    for(let item of t) {
        need.set(item, need.has(item) ? need.get(item) + 1 : 1)
    }
    
    // 初始化左右指针
    let l = 0
    let r = 0
    // 初始化res
    let res = ''

    let needSum = need.size
    while(r < s.length) {
        const c = s[r]
        // 如果存在当前字符
        if(need.has(c)) {
            need.set(c, need.get(c) - 1)
            // 判断当前字符是否已经不需要
            if(need.get(c) === 0) {
                needSum--
            }
        }
        // 如果所需字符已经全部到位
        while(needSum === 0) {
            // 更新res
            res = (!res || res.length > (r - l + 1)) ? s.substring(l, r + 1) : res
            const c2 = s[l]
            // 更新needSum
            if(need.has(c2)) {
                need.set(c2, need.get(c2) + 1)
                if(need.get(c2) === 1) {
                    needSum++
                }
            }
            l++
        }
        r++
    }
    return res
};

在这里插入图片描述

相对于法一的优化

  • 时间复杂度优化:如何判断滑动窗口是否覆盖t中所有字符?法一重新进行遍历记总和,但是法二是过程中顺便判断记总和。
  • 空间复杂度优化:法一使用了两个字典,法二只使用了一个字典。。

思路:滑动窗口

  1. 建立左右指针
  2. 先保持左指针不懂,移动右指针。扩大滑动窗口范围,直至包含t中所有字符。
  3. 移动左指针,右指针保持不动。减小滑动窗口范围,直至不包含t中所有字符。
  4. 当不包含t中所有字符,重复第2步,直至右指针移动到s串末尾。

时间空间复杂度-法二

  1. 时间复杂度:O(n + m)
  2. 空间复杂度:O(m)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值