Find length of smallest window that contains all the characters of a string in another string

前端 未结 8 1502
不思量自难忘°
不思量自难忘° 2020-12-24 09:05

Recently i have been interviewed. I didn\'t do well cause i got stuck at the following question

suppose a sequence is given : A D C B D A B C D A C D and search seq

8条回答
  •  攒了一身酷
    2020-12-24 09:41

    Here is my solution in Python. It returns the indexes assuming 0-indexed sequences. Therefore, for the given example it returns (9, 11) instead of (10, 12). Obviously it's easy to mutate this to return (10, 12) if you wish.

    def solution(s, ss):
        S, E = [], []
        for i in xrange(len(s)):
            if s[i] == ss[0]:
                S.append(i)
            if s[i] == ss[-1]:
                E.append(i)
        candidates = sorted([(start, end) for start in S for end in E
                            if start <= end and end - start >= len(ss) - 1],
                            lambda x,y: (x[1] - x[0]) - (y[1] - y[0]))
        for cand in candidates:
            i, j = cand[0], 0
            while i <= cand[-1]:
                if s[i] == ss[j]:
                    j += 1
                i += 1
            if j == len(ss):
                return cand
    

    Usage:

    >>> from so import solution
    >>> s = 'ADCBDABCDACD'
    >>> solution(s, 'ACD')
    (9, 11)
    >>> solution(s, 'ADC')
    (0, 2)
    >>> solution(s, 'DCCD')
    (1, 8)
    >>> solution(s, s)
    (0, 11)
    >>> s = 'ABC'
    >>> solution(s, 'B')
    (1, 1)
    >>> print solution(s, 'gibberish')
    None
    

    I think the time complexity is O(p log(p)) where p is the number of pairs of indexes in the sequence that refer to search_sequence[0] and search_sequence[-1] where the index for search_sequence[0] is less than the index forsearch_sequence[-1] because it sorts these p pairings using an O(n log n) algorithm. But then again, my substring iteration at the end could totally overshadow that sorting step. I'm not really sure.

    It probably has a worst-case time complexity which is bounded by O(n*m) where n is the length of the sequence and m is the length of the search sequence, but at the moment I cannot think of an example worst-case.

提交回复
热议问题