Minimum Window Substring - Airbnb Interview Question - Leetcode 76

Ғылым және технология

🚀 neetcode.io/ - A better way to prepare for Coding Interviews
🐦 Twitter: / neetcode1
🥷 Discord: / discord
🐮 Support the channel: / neetcode
Code on Github: github.com/neetcode-gh/leetco...
💡 CODING SOLUTIONS: • Coding Interview Solut...
💡 DYNAMIC PROGRAMMING PLAYLIST: • House Robber - Leetco...
🌲 TREE PLAYLIST: • Invert Binary Tree - D...
💡 GRAPH PLAYLIST: • Course Schedule - Grap...
💡 BACKTRACKING PLAYLIST: • Word Search - Backtrac...
💡 LINKED LIST PLAYLIST: • Reverse Linked List - ...
Problem Link: neetcode.io/problems/minimum-...
0:00 - Read the problem
1:15 - Drawing Brute Force
5:55 - Drawing Linear Solution
17:45 - Coding Solution
leetcode 76
This question was identified as an airbnb interview question from here: github.com/xizhengszhang/Leet...
#airbnb #python #slidingwindow
Disclosure: Some of the links above may be affiliate links, from which I may earn a small commission.

Пікірлер: 287

  • @NeetCode
    @NeetCode3 жыл бұрын

    🚀 neetcode.io/ - I created a FREE site to make interview prep a lot easier, hope it helps! ❤ This one turned out longer than expected, recommend 1.5x speed

  • @thelookofdisapproval8234

    @thelookofdisapproval8234

    3 жыл бұрын

    Lmao I've watched so many lectures on youtube, many require you see it at 1.5x or 2x this is the first time, I have seen the youtuber himself suggesting doing the same, thank you for the uploads also can you do a question on union find algo?

  • @brieflyfun

    @brieflyfun

    Жыл бұрын

    I wonder if there's a problem in line 26~28. The "have" count is not balanced. Should we have the same == conditions as line 17? if s[l] in countT and windows[s[l]] == countT[s[l]]: have -= 1 windows[s[l]] -= 1 to make the count balanced. "have" is the number of unique characters that meets the required numbers.

  • @Axl124124

    @Axl124124

    11 ай бұрын

    to be honest if I meet you and I hear you talking at normal speed, I would think you have dementia or something. I watch all your videos at 2x speed.

  • @jackscott4829
    @jackscott48292 жыл бұрын

    I never thought I'd see the day I'd fully understand a Leetcode Hard problem in under a half hour. Bless you sir.

  • @harshitsingh2118

    @harshitsingh2118

    Жыл бұрын

    same feeling 🥳

  • @srikrishnarohanmadiraju8688
    @srikrishnarohanmadiraju86883 жыл бұрын

    Beautiful..! The blackboard explanation, clean code, the naming conventions, everything is just beautiful. Thank you.

  • @srinadhp
    @srinadhp2 жыл бұрын

    as usual, your explanation makes a hard problem look simple! Thank you so much!

  • @trueworth638
    @trueworth6383 жыл бұрын

    great video, you're helping out a ton for interviews! Your channel is too underrated

  • @airysm
    @airysm3 жыл бұрын

    Thank you for these videos! I'm switching from java to python for interviews and these have been really helpful

  • @NeetCode

    @NeetCode

    3 жыл бұрын

    Thanks! I'm really happy that they are helpful!

  • @kiddem9247
    @kiddem9247 Жыл бұрын

    For your solution, in line 17 comparing both map counts: if c in countT and window[c] == countT[c] I had to replace == with

  • @re-think7693

    @re-think7693

    Жыл бұрын

    Thanks! This helps

  • @yaboyjz

    @yaboyjz

    Жыл бұрын

    same here!

  • @brieflyfun

    @brieflyfun

    Жыл бұрын

    "need" is number of unique characters in t, not total number of characters. I think the problem is in line 26~28, which should have the same == conditions as line 17. if s[l] in countT and windows[s[l]] == countT[s[l]]: have -= 1 windows[s[l]] -= 1 to make the count balanced. "have" is the number of unique characters that meets the required numbers.

  • @felixdeng9824

    @felixdeng9824

    8 ай бұрын

    In his solution, he set need = len(countT) instead of len(t), that makes the while loop( have == need) will execute with no errors. Of course we can set window[c]

  • @govindrai93

    @govindrai93

    8 ай бұрын

    thanks @felixdeng9824 that was the reason. in python len(dictionary) == number of keys. So the case of "aaa", the dictionary would be { "a": 3 } and the len(a) would be 1. That's why == works for neetcode!

  • @SanketBhat7
    @SanketBhat75 ай бұрын

    This is one of a very few hard leetcode problems that I solved without any help. But indeed this video dives to the depth of the concept, watching to understand what other ways I could have solved it. Thanks!!

  • @tanaykamath1415
    @tanaykamath1415 Жыл бұрын

    I love how he explains and codes stuff elegantly !

  • @infinity-clips9414
    @infinity-clips9414 Жыл бұрын

    The legend comes and make it a piece of cake as usual. Your explanations are way more on another level. thanks man.

  • @roman_mf
    @roman_mf Жыл бұрын

    Thanks to you and NeetCode 150 list, I was able to solve this hard problem on my own without peeking at your solution! The runtime sucked really bad, but it was nonetheless accepted. Hooray!

  • @ChanChan-pg4wu
    @ChanChan-pg4wu2 жыл бұрын

    Likely the most difficult question for sliding window. Again, watched it 3 times. Thank you, Neet!

  • @Notezl

    @Notezl

    Жыл бұрын

    wait till you try Sliding window Maximum.

  • @Haseebkhan-yd9ud

    @Haseebkhan-yd9ud

    Жыл бұрын

    @@Notezl 😂

  • @Tyler-jd3ex

    @Tyler-jd3ex

    10 ай бұрын

    I’m glad that somebody else had to watch it multiple times. Sometimes I just feel dumb, but I have to remind myself that these videos are prepared and Neetcode probably struggled with it himself at first.

  • @akagamishanks7991

    @akagamishanks7991

    7 ай бұрын

    tbh honest Sliding window Maximum way easier haahahah@@Notezl

  • @alejandrodardon7091
    @alejandrodardon70912 жыл бұрын

    Amazing video with a great efficient solution, despite the code being a little bit all over the place at first glance I could perfectly follow your explanation great job!

  • @gouravsingh9509
    @gouravsingh95092 жыл бұрын

    Thanks a lot for the amazing explanation with each video. It has been so useful to understand things.

  • @hmanjun7260
    @hmanjun7260 Жыл бұрын

    Nice. I figured out how we would need to slide the window, but was struggling with how to store it and check if it satisfies the t counts. This was the first leetcode hard I've tried and feel good that I got a majority of the idea down.

  • @SIAMEInekeidijdnen

    @SIAMEInekeidijdnen

    Жыл бұрын

    I agree. It was not as hard to actually figure out what was needed to solve the problem, but then when it came to implementing the solution, it got really hard and confusing. It was probably because we were trying to implement the brute force approach.

  • @Yuipser
    @Yuipser2 жыл бұрын

    Thanks for another greater video ! after watching your video on 567. Permutation in String, I could figure out solution for this by myself. ur videos are always very inspiring and helpful ! also, just want to share how I handle the left position differently: 1. keep the left pointing at a char in t, 2. if countS[nums[l] ] > countT[nums[l] ], increase the left because we can shorten the substring by making left pointing at the next num in t these condition will make sure that redundant elements on the left are dropped before we calculate the len of current substring

  • @sar3388

    @sar3388

    26 күн бұрын

    Were you able to code it yourself?

  • @tomonkysinatree
    @tomonkysinatree3 ай бұрын

    Glad you made this solution video. I felt like i was close but wasn't able to test all the test cases. My approach was slightly off by trying to perform the solution with a single hashmap. Made some of the conditionals/updating super confusing and complicated and would have not finished in a real interview.

  • @mingjuhe1514
    @mingjuhe15142 жыл бұрын

    Thanks bro ,I am keeping watching your video these days. I feel I am improving in a fast speed.

  • @DavidInga7
    @DavidInga7 Жыл бұрын

    @NeetCode Thanks so much for the video! I enjoyed watching how you solved this problem. I wanted to point out that the Leetcode problem states that we are dealing with only the 26 lowercase characters in the English alphabet. As such, our hashmap would be a constant size. Therefore our hashmap would never grow to a size of 100 as stated. All your videos are awesome. I hope my comment is constructive and helpful!

  • @user-SerhijA

    @user-SerhijA

    8 ай бұрын

    That's not the case. Even at the beginning of the video it is seen that letters are capital.

  • @yuxuanc
    @yuxuanc2 жыл бұрын

    Best explanation ever for this question, thank you for the great work!

  • @mohamadilhamramadhan6354
    @mohamadilhamramadhan6354 Жыл бұрын

    It tooks me 12 hours to solve hard problem for the first time lol. I even use a linked list haha. And the runtime is bad but at least pass the test. I always look at your solution after I try by myself and... I often learn something new. Thanks 👍

  • @sravanikatasani6502
    @sravanikatasani65023 жыл бұрын

    Hello, Bob Ross!. Amazing explanation as always :)

  • @SeifGneedy2
    @SeifGneedy2 Жыл бұрын

    Great Explanation, Thanks for everything! a small note: if we checked the entire hashmap, it will also be in O(1) because we have just lowercase and uppercase characters (Just 52 entries in the hashmap).

  • @algopenne

    @algopenne

    19 күн бұрын

    agreed, I think something like this works just as well (at least it passes on LC) class Solution: def minWindow(self, s: str, t: str) -> str: t_counts = {} w_counts = {} #window_counts for c in t: t_counts[c] = t_counts.get(c, 0) + 1 w_counts[c] = 0 l = 0 r = 0 def is_good_window(): for c in t_counts: if w_counts[c] return False return True curr_len = len(s) + 1 res = "" while r w_counts[s[r]] = w_counts.get(s[r], 0) + 1 while is_good_window(): if r - l + 1 curr_len = r - l + 1 res = s[l:r+1] w_counts[s[l]] -= 1 l += 1 r += 1 return res

  • @garitina987
    @garitina987 Жыл бұрын

    Wow that was incredibly explained. Hats off once again.

  • @TheSSB007
    @TheSSB0073 жыл бұрын

    Loved the explanation!!

  • @shriharihallur633
    @shriharihallur6333 жыл бұрын

    Very well explained, Thanks!!

  • @RanjuRao
    @RanjuRao Жыл бұрын

    Thank you for your detailed explanation on the approach!

  • @lomoyang3034
    @lomoyang30342 жыл бұрын

    Another issue. In your code, the window map also count the character which does not exist in T, which is different from what you explained in slides. AFAK, your implementation is different than what you explained, though both of them are sliding window.

  • @bas5rocker311

    @bas5rocker311

    Жыл бұрын

    no, I think the implementation matches the code. Try and pop from left. If the character popped is not in 't', it throws a key error

  • @amrutaj28

    @amrutaj28

    Жыл бұрын

    I was going to point out the same. In both push (for right char) and pop (for left char) places, we need to just push the chars that exist in t-map. And this code modification works perfectly according to his explanation. PS. Thank you so much Neetcode for this great video!

  • @hwang1607

    @hwang1607

    9 ай бұрын

    this is my modified solution using the explanation, the curr hashmap is a map of the chars in T all set to 0, and chars not in T are not added from S class Solution: def minWindow(self, s: str, t: str) -> str: tcount = collections.Counter(t) curr = {k : 0 for k in t} l = 0 have = 0 need = len(tcount) minlen = float('inf') res = [0,0] for r in range(len(s)): if s[r] in curr: curr[s[r]] += 1 if curr[s[r]] == tcount[s[r]]: have += 1 while have == need: if (r - l + 1) minlen = min(minlen, r - l + 1) res = [l,r] if s[l] in curr: curr[s[l]] -= 1 if curr[s[l]] have -= 1 l += 1 if minlen == float('inf'): return "" l,r = res return s[l:r+1]

  • @vikhyatsharma5035
    @vikhyatsharma50353 жыл бұрын

    Thank you, loved the explanation

  • @hoyinli7462
    @hoyinli74622 жыл бұрын

    finally you uploaded your code! thx

  • @MP-ny3ep
    @MP-ny3ep5 ай бұрын

    Phenomenal explanation !!!! Thank you sooo very much !!!

  • @ishanpadalkar9072
    @ishanpadalkar90725 ай бұрын

    I had this solved intuitively, in quadratic time i'm guessing which is why it was failing the last test case on Time. This is a great explanation 👍

  • @atulkumar-bb7vi
    @atulkumar-bb7vi Жыл бұрын

    Very nicely explained the hard problem. Really liking your videos. Thanks for this...

  • @cathyhuang8557
    @cathyhuang85573 жыл бұрын

    Thank you very much~ It is really helpful~

  • @mruduladdipalli5417
    @mruduladdipalli5417 Жыл бұрын

    With new test case of s = "bbaa" , t = "aba", it's not going inside while loop because "need" end up to 3 and "have" end up with 2, Since we check for map values of currentChar, which is updating have to 3, but need is at 3, because of length of t, it has 3 as value so we need to update "need" = uniqueCharactersOf(t); which we can achieve using set

  • @davidmar8612

    @davidmar8612

    Жыл бұрын

    I made this change and it worked thank you!

  • @yinglll7411
    @yinglll74112 жыл бұрын

    Thank you for the explanation!

  • @kafychannel
    @kafychannel Жыл бұрын

    Thank you NeedCode I got the entire idea of your solution!

  • @rishikeshjaadhav2405
    @rishikeshjaadhav2405 Жыл бұрын

    Awesome explanation! Thank you!

  • @thinhnguyenvan7003
    @thinhnguyenvan7003 Жыл бұрын

    Hi NeetCode. Your video is amazing and helped so much in some problems that i did not have any idea to solve. By the way, when you apply in GG, are there any other questions related about System Design or something like that?

  • @satishrella4800
    @satishrella48002 жыл бұрын

    Thanks for your videos, actually you do a great explanation, I have one doubt about this problem what if we have two results and print the result lexicographically which is smaller. if possible can you add this part? Thanks in advance

  • @bouzie8000
    @bouzie80006 ай бұрын

    This was so so fun lol. You're doing the lord's work

  • @yangjiawenxu2319
    @yangjiawenxu2319 Жыл бұрын

    It is very clear for a hard leetcode problem.

  • @linli7049
    @linli70493 жыл бұрын

    You are such a good trainer

  • @harshavardhanranger
    @harshavardhanranger2 жыл бұрын

    whenever I find your video for a question I'm looking for, happiness = float("inf") !!

  • @aryanyadav3926
    @aryanyadav39262 жыл бұрын

    Wonderful explanation!

  • @vishnusunil9610
    @vishnusunil96108 ай бұрын

    thank you for the crystal clear explanation and code

  • @anonymoussloth6687
    @anonymoussloth66872 жыл бұрын

    At 19:17 why is need set to size of countT? If t is "aab" then we would need 2 a and 1b right? But ur code will set need at 2

  • @sucraloss
    @sucraloss10 ай бұрын

    I was so close on this one! I didn't think of that have vs need thing you did so I only solved half the test cases because I moved the left pointer properly but only changed the min_window_size when the count in t matched the count in s, which failed cases where we had duplicates. I couldn't think of a quick way to compare that we have enough of each specific value in the "s" hashmap such that we could check to see if our window was smaller. I was thinking maybe you would just have to make a helper function to go through the "s" hashmap's keys and make sure you have at least enough of each letter to match the count in t, but making a single integer increment when you have enough for each key is smart. I didn't even bother implementing my hashmap count helper function because I figured I was missing some way of tracking the size of the s and t hashmap and didn't want to spend 20 minutes just to get to a TLE. *Edit: I actually did check to see how slow my helper function would be and it was 80% slower than NC's solution. So it could work to check through the hashmaps key by key each time, but it's super slow. Good to know I was really very close to solving this.

  • @anjumanislamiachhatarpur7093
    @anjumanislamiachhatarpur7093 Жыл бұрын

    Nice explanation. Appreciate your effort.

  • @arupdas2210
    @arupdas22102 жыл бұрын

    C++ Implementation of the above explanation: string minWindow(string s, string t) { if(t==""){ return ""; } unordered_map um; for(int i=0;i

  • @riteshsrivastava3447

    @riteshsrivastava3447

    9 ай бұрын

    Thanks , this helps .

  • @uvarajupdates4464
    @uvarajupdates44645 ай бұрын

    Keep posting videos regularly brother❤

  • @jegadheeswarank6290
    @jegadheeswarank62904 ай бұрын

    Awesome! Now I am going to code this in Java 🙂

  • @shashwatkumar6965
    @shashwatkumar69652 жыл бұрын

    If String t has repeated characters, then instead of have+= 1 we can do have += countT[c] and similarly instead of have -= 1, we do have -= countT[s[l]]

  • @business_central
    @business_central Жыл бұрын

    Can anyone tell me why we don't need to initialize the right pointer ? Just doing for r in range(Len(s)) is enough ? I'm a bit confused

  • @biswaMastAadmi
    @biswaMastAadmi2 жыл бұрын

    Enjoyed the solution !

  • @feesabilillah101
    @feesabilillah1019 ай бұрын

    The best explanation hands down

  • @thesouthsidedev1812
    @thesouthsidedev1812 Жыл бұрын

    This was a very well explained solution

  • @kchemutai3483
    @kchemutai3483Ай бұрын

    Best explanation ever. Thank you neetcode

  • @scottishfoldmocha5875
    @scottishfoldmocha58752 жыл бұрын

    19:46 - why are we updating window hash map before checking if that character is in countT hash map? Aren't we only storing and counting characters we need and not ALL characters?

  • @wangfred
    @wangfred Жыл бұрын

    When it runs, it runs like Rolex, so intriguing and accurate! There are two windows actually: the l r and have need.

  • @sastecoder3421
    @sastecoder3421 Жыл бұрын

    Lord NeetCode SUPREME Teacher !!

  • @lymmontijo87
    @lymmontijo874 ай бұрын

    WOW! Thanks for the explanation.

  • @275phuongvy
    @275phuongvy3 жыл бұрын

    thank you. great explanation

  • @sumitsharma6738
    @sumitsharma6738 Жыл бұрын

    398 ms done. Took me 40 minutes to do so. but now lets see optimised version too :)

  • @chillsjiujitsu
    @chillsjiujitsu5 ай бұрын

    Neetcode is the goat!!!

  • @greatred2558
    @greatred25582 жыл бұрын

    hey have one question how is your code working for test case "aa" "aa". since in that case has will only be updated once and hence result will not be updated ? I was able to submit by modifying my condition to increment have.

  • @rupeshpatil3429

    @rupeshpatil3429

    2 жыл бұрын

    he is setting need to len(countT) not len(t) so in the case of "aa" need will be set to 1 but inside countT dict "a" index will have value 2.

  • @sports_op
    @sports_op Жыл бұрын

    Whoa, it's something I'd never have come up with by myself 🔥

  • @chandrachurmukherjeejucse5816
    @chandrachurmukherjeejucse5816 Жыл бұрын

    Great Explanation!!!

  • @hix0071
    @hix00712 жыл бұрын

    well done. very well presented. 10/10

  • @imranimmu4714
    @imranimmu4714 Жыл бұрын

    Great Explanation Thank you

  • @joshstevens2779
    @joshstevens2779 Жыл бұрын

    I didn't do this in Python but another language. There seems to be an issue with line 16. The condition should be checking if window[c]

  • @najwadafir217

    @najwadafir217

    10 ай бұрын

    yes, you're right! else, the test s = "aa" t = "aa" won't pass

  • @jey_n_code

    @jey_n_code

    10 ай бұрын

    thats true, thank youuu!

  • @straightlegmusic5269

    @straightlegmusic5269

    9 ай бұрын

    Was watching this on the neetcode site, literally opened it up to scan the comments for this issue cause I was doing this with JS lol.

  • @cinimodmil

    @cinimodmil

    8 ай бұрын

    thanks for this! so in this case, have represents the number of characters needed to build substring t? i.e., we take into account duplicates when counting too.

  • @mondayemmanuel191
    @mondayemmanuel1912 жыл бұрын

    Great explanation.

  • @devanshprakash8354
    @devanshprakash83542 жыл бұрын

    Does window map insert all elements from or only those which are present in t??

  • @aadil4236
    @aadil42362 жыл бұрын

    ingenious explanation!!

  • @arsahilar
    @arsahilar8 ай бұрын

    Neatly Explained!!

  • @b9944236
    @b9944236 Жыл бұрын

    Great explanations.

  • @aakashs1806
    @aakashs1806Ай бұрын

    Great Video

  • @zr60
    @zr602 жыл бұрын

    at 13:58 when have[b] == 2 becomes have[b] == 1 == need[b] , doesn't it satisfy the condition? How do you prevent it from not incrementing unnecessarily?

  • @everydaystatus158

    @everydaystatus158

    2 жыл бұрын

    Exactly I am also wondering about that condition

  • @JulianBoilen
    @JulianBoilen2 жыл бұрын

    7:56 Comparing the maps It wouldn't be bounded by t, it would be bounded by the number of possible characters, which is 52, or O(1).

  • @milapshah1075

    @milapshah1075

    2 жыл бұрын

    I agree code needs one modification where you count need

  • @sudhanshusingh-cy9wp

    @sudhanshusingh-cy9wp

    Жыл бұрын

    this might be submitted, but in an interview, this solution will considered much much better, i think

  • @assaidy
    @assaidyАй бұрын

    great explanation

  • @bostonlights2749
    @bostonlights274910 ай бұрын

    I was asked this in an interview yesterday

  • @Spham99
    @Spham992 жыл бұрын

    doing java in interviews always messes me up, I get so caught up in trying to figure out how to do the little things :/ I tried switching to Python for that, but that messed me up too trying to learn a different language

  • @vatsaplaysguitar
    @vatsaplaysguitar10 ай бұрын

    what is the reason for updating res before returning? (line 31)

  • @damirsharip604
    @damirsharip60410 ай бұрын

    You are the best of bests

  • @user-cw3jg9jq6d
    @user-cw3jg9jq6d5 ай бұрын

    Hello a nd thank you so mcuh for tutorial. Is there somewhere I can share my code with you or others and get help? I need to write it in Java first, but I am trying to become better with python.

  • @zhengzuo5118
    @zhengzuo51182 жыл бұрын

    Very good explanation

  • @pinakadhara7650
    @pinakadhara7650 Жыл бұрын

    Thanks for this! Even though I understood how this particular problem is solved, I am not able to get the "intuition" or "trick" of this approach for some reason.

  • @thisisnotok2100
    @thisisnotok21008 ай бұрын

    I improved upon this by using a single hash map containing the amount of characters in the t string, and decrementing the value as it was encountered in s. As we closed the window, we would re increment back up. If all the values in the map were 0 or less, we knew we had a valid solution.

  • @sumeshbharathiramasamy8559

    @sumeshbharathiramasamy8559

    7 ай бұрын

    checking the whole map for 0 or less --> will take 0(m). In the video, the approach has 0(1) time complexity

  • @kareni7572
    @kareni7572Ай бұрын

    Thank you for showing why we need that extra variable on top of hashmap & also that it will only be updated if it is ==

  • @danomov
    @danomov15 күн бұрын

    To optimize our window shrinking process, I think we can store the index of the new valid value. This way, next time we can move our start point directly to the next valid value and begin counting from there.

  • @danny65769
    @danny65769 Жыл бұрын

    Should line 27 be: if s[l] in countT and window[s[l]] + 1 == countT[s[l]]: ? Otherwise, if we try to shrink the window twice with the same left character, the "have" would be decremented twice for the same character?

  • @danny65769

    @danny65769

    Жыл бұрын

    I realized once "have" is decremented, the while condition of "have == need" would fail. So "have" would not be decremented more than once for the same character during any one window shrink operation.

  • @harishankarkarthik3570
    @harishankarkarthik35705 ай бұрын

    I solved this by myself, before seeing this solution. And I checked here to know that it was the most optimal solution :)

  • @amitgupta1202
    @amitgupta12029 күн бұрын

    you can check dictionary sizes if we treat absence as zero. Complexity remains same but less no variables

  • @fortuner1122
    @fortuner112210 күн бұрын

    excellent solution

  • @RajiurRahman1
    @RajiurRahman1 Жыл бұрын

    Hey! Big fan here. Just wanted to point out, this code will fail for s= "bbaa" and t="aba" (Leetcode testcase). In line#17, you have the following (C# version) if (countT.find(c) != countT.end() && window[c] == countT[c]) For the aforementioned test scenario, countT['a'] is 2, while looping through s, for the first time when it sees 'a', window[c] == countT[c] this part of the statement will become false, hence have will not be increased. After finishing the first for loop, count will be 3 and have will be 2. If there are 10 'a's in s and 10 'a's in t, the have will have 1 after the loop. Therefore it'll return an empty string "". Changing it to following will pass the case. window[c]

  • @JanneSauvala
    @JanneSauvala11 ай бұрын

    On line 10, it should be "have, need = 0, sum(countT.values())" because if a character is present more than once in t, then it is not enough to check the length of the dictionary (number of keys) but you would need to sum up the dict's values.

  • @xmnemonic
    @xmnemonic Жыл бұрын

    It's a good solution, but I think it's very unlikely anyone could come up with the have, need variables in an interviewing first time seeing this problem. That is a very clever optimization.

  • @AlessioPepe
    @AlessioPepe3 ай бұрын

    I wan't able to figure out how to reduce complexity from O(n*k) to O(n). Thank you for explaining the trick so easily!

  • @HuyLe-tu4pj
    @HuyLe-tu4pj5 ай бұрын

    good job bro

  • @MrTulkonas
    @MrTulkonas9 күн бұрын

    I suggest an alternative solution, which I think it is far easier to understand and code. 1. Start defining a goal dictionary (with counts of "t" string) and worst dictionary (with counts of "s" string) -> current solution. 2. Check if solution valid (counts in goal smaller or equal to current solution) => solution exists, else return "". 3. Initiate a while loop with pointer l, r = 0, len(s) -1 3a If utmost left character not in goal, increase l. 3b Elseif utmost left character in goal and its count in current solution bigger than in goal, increase l and reduce count in current solution by 1 3c,3d elseif ... (analog for utmost right character) 3e return s[l:r+1] 4 return s[l:r+1]

Келесі