Dashboard Temp Share Shortlinks Frames API

HTMLify

minimum-window-subsequence.py
Views: 4 | Author: prakhardoneria
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Solution:
    def minWindow(self, s1, s2):
        n1, n2 = len(s1), len(s2)
        i, j = 0, 0
        min_len = float('inf')
        start_idx = -1
        
        while i < n1:
            if s1[i] == s2[j]:
                j += 1
                if j == n2:
                    end = i
                    j -= 1
                    while j >= 0:
                        if s1[i] == s2[j]:
                            j -= 1
                        i -= 1
                    i += 1
                    j += 1
                    
                    if end - i + 1 < min_len:
                        min_len = end - i + 1
                        start_idx = i
            i += 1
            
        return "" if start_idx == -1 else s1[start_idx:start_idx + min_len]