题解 | #最小覆盖子串#

最小覆盖子串

http://www.nowcoder.com/practice/c466d480d20c4c7c9d322d12ca7955ac

#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param S string字符串 
# @param T string字符串 
# @return string字符串
#
import sys
class Solution:
    def minWindow(self , S: str, T: str) -> str:
        # write code here
        need_window = {}
        satisfy_window = {}
        need_degree = len(T)
        satisfy_degree = 0
        for c in T:
            need_window[c] = need_window.get(c,0) + 1
        res = sys.maxsize
        start,end=-1,-1
        i,j = -1,-1
        while j<len(S)-1:
            j+=1
            c = S[j]
            if c not in need_window:
                continue
            satisfy_window[c] = satisfy_window.get(c,0) + 1
#             print(satisfy_window)
            if satisfy_window[c] <= need_window[c]:
                satisfy_degree += 1
                if satisfy_degree == need_degree:
                    while satisfy_degree == need_degree:
                        i += 1
                        c = S[i]
                        if c not in need_window:
                            continue
                        satisfy_window[c] -= 1
                        if satisfy_window[c]<need_window[c]:
                            satisfy_degree -= 1
                    if j-i<res:
                        start = i
                        end = j
                        res = j-i
        return S[start:end+1]
                        
                    
全部评论

相关推荐

06-05 19:46
已编辑
武汉大学 后端
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-15 22:48
牛马人的牛马人生:建议就是把北邮几个字放大就行了。北邮本硕按理来说完全不用担心啊
点赞 评论 收藏
分享
评论
1
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务