How to find smallest substring which contains all characters from a given string?

后端 未结 15 1136
灰色年华
灰色年华 2020-12-02 07:36

I have recently come across an interesting question on strings. Suppose you are given following:

Input string1: \"this is a test string\"
Input strin         


        
15条回答
  •  感情败类
    2020-12-02 08:04

    def minimum_window(s, t, min_length = 100000):
        d = {}
        for x in t:
            if x in d:
                d[x]+= 1
            else:
                d[x] = 1
    
        tot = sum([y for x,y in d.iteritems()])
        l = []
        ind = 0 
        for i,x in enumerate(s):
            if ind == 1:
                l = l + [x]
            if x in d:
                tot-=1
                if not l:
                    ind = 1
                    l = [x]
    
            if tot == 0:
                if len(l)

提交回复
热议问题