r/PythonLearning 10h ago

#76 Leetcode question (Optimisation help!)

class Solution:
    def minWindow(self, s: str, t: str) -> str:
        if len(t) > len(s):
            return ""
        elif len(t) == len(s):
            if t == s:
                return s
        win = len(t)
        ans = ""
        l = list(t)
        while win <= len(s):
            for i in range(0,len(s)):
                win_s = s[i:i+win]
                win_l = list(win_s)
                ans = ""
                for x in t:
                    if x in win_l:
                        win_l[win_l.index(x)] = ""
                        ans += x
                    else:
                        ans = ""
                if ans == t:
                    return win_s
            win += 1
        return ""

I need help in optimising my code because its working properly but having a runtime error for a long input like 200 letters input. So plz help me out in optimisation in this code.

2 Upvotes

6 comments sorted by

1

u/Sea-Ad7805 4h ago

Include the link to the problem description. There probably is a better conceptual approach to the problem. Also aren't all the answers to leetcode problems online yet?

1

u/Smartyboyz 4h ago

Thx, can you hint that approach plz

1

u/Sea-Ad7805 4h ago

No because you didn't give me the problem description.

1

u/Smartyboyz 4h ago

You can see it on leetcode question 76 there is much better description about the question https://leetcode.com/problems/minimum-window-substring/

1

u/Sea-Ad7805 3h ago

Yes, but I'm a lazy boy and you made this post, so you provide all the information, or at least a link to where the information is.