LeetCode Longest Repeating Character Replacement Solution Explained - Java

Поделиться
HTML-код
  • Опубликовано: 22 авг 2024
  • The Best Place To Learn Anything Coding Related - bit.ly/3MFZLIZ
    Join my free exclusive community built to empower programmers! - www.skool.com/...
    Preparing For Your Coding Interviews? Use These Resources
    --------------------
    (My Course) Data Structures & Algorithms for Coding Interviews - thedailybyte.d...
    AlgoCademy - algocademy.com...
    Daily Coding Interview Questions - bit.ly/3xw1Sqz
    10% Off Of The Best Web Hosting! - hostinger.com/...
    Follow Me on X/Twitter - x.com/nickwhit...
    Follow My Instagram - / nickwwhite
    Other Social Media
    ----------------------------------------------
    Discord - / discord
    Twitch - / nickwhitettv
    TikTok - / nickwhitetiktok
    LinkedIn - / nicholas-w-white
    Show Support
    ------------------------------------------------------------------------------
    Patreon - / nick_white
    PayPal - paypal.me/nick....
    Become A Member - / @nickwhite
    #coding #programming #softwareengineering

Комментарии • 133

  • @taiyuguo6672
    @taiyuguo6672 3 года назад +95

    One thing that isn't well explained is that max_count keeps track of the rolling max frequency of characters from index 0 to i. It does not keep track of the maximum frequency of characters in the window, which is what "window_end - window_start - max_count + 1 > k" is checking. This works fine, but the reason this works isn't trivial and definitely deserves a better explanation.

    • @andywang4189
      @andywang4189 2 года назад +10

      Yes, I'm also wondering this. max_count never decreases when window_start slides to the right.

    • @chachacool3788
      @chachacool3788 Год назад +3

      I was wondering about the same thing. The simple equation felt like black magic. So here's how I understood it. maxFreq is actually keeping count of the maxFreq(lets call this maxFreq1) of a letter seen so far. Why we need this and not the actual maxFreq1 in the rolling window you may ask? Cuz we know that with that maxFreq the window size can be maxFreq+k at min. So it doesnt really matter until you encounter another substring which has more frequency and satisfies that equation. j-i+1 - maxFreq

    • @mastermax7777
      @mastermax7777 Год назад +4

      yea, why isnt he recalculating maxFrequency when hes sliding the window right..

  • @JDzDesigns
    @JDzDesigns 4 года назад +74

    Hey, Nick. Just a thought, I think the '+ 1' in these equations is to account for zero-indexing. Because 2 - 0 = 2 but array[0] + array[1] + array[2] = size of 3

    • @cloud5887
      @cloud5887 4 года назад +12

      Exactly right. It is not because he is adding a new letter, it is because our pointers are zero based.

    • @zzzzzalanzzzzz
      @zzzzzalanzzzzz 4 года назад +7

      I don't think it is because of the zero-indexing. From my point of view It's because we are subtracting the starting point. It's something similar with months. How many months are there from January (1) to June (6)? If we do the subtraction 6-1=5, but I'm not taking in count January, so I have to add 1 to get 6

    • @adityakumaaar
      @adityakumaaar 4 года назад +2

      @@zzzzzalanzzzzz Yeah, imo you're correct.

    • @rishabhmalhotra1317
      @rishabhmalhotra1317 3 года назад +3

      @@zzzzzalanzzzzz This has to be the dumbest explanation, it is because its arrays are zero indexed

    • @saraleo3348
      @saraleo3348 3 года назад

      @Alan Serrano yeah you're right. For those who might be confused, consider two arrays one with 0-indexing and other with 1-indexing, [0,1,2,3] and [1,2,3] -> in both cases 3-1 would give 2, but actually there are 3 elements.

  • @apdr8783
    @apdr8783 3 года назад +6

    the key to this question is why you dont update the max_count when you shrink the window. I have watched several videos now, everyone just skip that part!

  • @benevolent6705
    @benevolent6705 4 года назад +17

    Why don't you have to recalculate max_count in the while loop? what if you pop a character off of the front, which was used to calculate max_count? In your code, max_count is simply the count of the most frequently seen character across the ENTIRE STRING not the window. Actually, I think this problem on leetcode is broken. The test cases are probably very simple, so everyone can get by with wrong code. Please correct me if I'm wrong.

    • @aniketsharma5221
      @aniketsharma5221 4 года назад

      same doubt

    • @paulengland8983
      @paulengland8983 4 года назад +1

      You can try your own test cases, no? But in this case, max is only changed/updated if a character is added, not subtracted. If you encounter the character again that you popped off, it will check (in the main while loop) if this visit will push the max up.

    • @itsbossable
      @itsbossable 4 года назад +5

      I had the same thought. But it is not required because if you look closely, size of the window never decreases. You can replace while with if and it will still work.

    • @Cube2deth
      @Cube2deth 4 года назад

      @@itsbossable i dont understand

    • @siddarthpandian5963
      @siddarthpandian5963 4 года назад +8

      @@Cube2deth Consider this example "AAABCDEFG" with k=2. The algorithm will increase the window size until it reaches a length of 5. i.e. the length of "AAABC". After this, the window size will remain at 5 until and unless we get a max_count that is greater than the previous. In this case we do not encounter a max_count that is greater than 3 and therefore the max_length will remain 5 for the rest of the iterations. I would advise to trace it and see for yourself.

  • @janmejaysingh7402
    @janmejaysingh7402 3 года назад +17

    When you are incrementing your window_start pointer and decrementing the count of that character(and lets assume that the first character was the max count so far,) how do you ensure that the char with highest count remains same?
    Am I making any sense.

    • @asmasadat8082
      @asmasadat8082 3 года назад +2

      yess. Have the same doubt

    • @menglishu4328
      @menglishu4328 3 года назад +7

      Because it has to be consecutive, when ur out of operation, the only thing you can do is to shrink the window size from left, and you already have the max length at this step. Since the max length will only be updated when a longer one is found, it doesn't matter if the highest freq char is the same or not.

    • @shiruliu9054
      @shiruliu9054 3 года назад +7

      Exactly! And the weirdest thing is nobody is mentioning this in the Leetcode discussion. I'm so confused.

    • @shalsteven
      @shalsteven 2 года назад

      @@menglishu4328 this is the most clear explanation that i found. Thank you

  • @rajankhatri3388
    @rajankhatri3388 4 года назад +3

    Simple form to understand this one
    private static int findLongRepCharRepl(String s, int k) {
    int[] counts_char = new int[26];
    int left =0;
    int max_count = 0;
    int result = 0;
    for(int right =0; right < s.length(); right++){
    int windowSize = right - left + 1;
    counts_char[s.charAt(right) - 'A'] = counts_char[s.charAt(right) - 'A'] + 1;
    int current_char_count = counts_char[s.charAt(right) - 'A'];
    max_count = Math.max(max_count , current_char_count);
    int replace_count = windowSize - max_count;
    if(replace_count > k){
    counts_char[s.charAt(left) - 'A'] = counts_char[s.charAt(left) - 'A'] - 1;
    left = left + 1;
    }else{
    result = Math.max(result,windowSize );
    }
    }
    return result;
    }

  • @manojboganadham9071
    @manojboganadham9071 3 года назад +10

    what about updating the max_count when we increment the window_start ?

  • @ishwarshelke128
    @ishwarshelke128 3 года назад +2

    doesnt matter how much questions u do at the end the way of ur explanation conveys the interviewer how much intelligent u r

  • @hippityhoppity6236
    @hippityhoppity6236 4 года назад +11

    And also after sliding the window why we are not updating the max_count since it might change depending on the character which corresponds to max_count.

    • @abhimanyusaini5
      @abhimanyusaini5 3 года назад +1

      well the inside while loop basically runs for 1 time only, so I think there is no point for updating the max_count.

    • @dkg4975
      @dkg4975 3 года назад +3

      that's exactly what I am thinking, if the max_count comes from window_start then we need to update the max_count to max_count - 1, isn't it ?

  • @ronakkenia4517
    @ronakkenia4517 4 года назад +8

    Thanks for the video! I think I understood, but it could help if you walked through the condition in the while loop more thoroughly. It is especially important for a test case like s = "BAAAB" and k = 2, where after we visit BAA, the max_count switches from B's count to A's count.

    • @GregDungca
      @GregDungca 4 года назад +2

      I was confused about this too. I think it makes more sense in the context of this approach to think of max_count as the max count of repeating characters across *all* sliding windows instead of the *current* sliding window.

    • @abhishekpathania8265
      @abhishekpathania8265 4 года назад +1

      While loop is used to decrease the window size and update char_count based on removed character. So, start will be incremented until we reach a point where window will become feasible again for max_count character.

  • @LT-wh7bl
    @LT-wh7bl 3 года назад +3

    Test case: s=ACBAD..........., k=2. When window_end is 4, which points to "D", we need to move window_start to the right. By following your code, window_start will be stoped at index 1, since 4 - 1 - 2 + 1 = 2 == k, but substring "CBAD" doesn't fulfill the requirement.

  • @ianpan0102
    @ianpan0102 4 года назад +1

    Here's a C++ implementation:
    """""""""""""""""""""""""""""""""""""""""""""""""
    class Solution {
    public:
    int characterReplacement(string s, int k) {
    int n = (int)s.length();
    unordered_map count; // character frequencies in the sliding window
    int max_length = 0; // longest consecutive substring (after operations) so far
    int max_count = 0; // most frequent char occurence in the sliding window
    // j: sliding window start, i: sliding window end
    for (int i = 0, j = 0; i < n; ++i) { // 'i' loops through the string normally
    count[s[i]]++; // update current char count
    max_count = max(max_count, count[s[i]]); // whether current char has higher freq
    while (i - j + 1 - max_count > k) { // compare no. of operations we must do with k
    count[s[j]]--; // decrease s[j]'s freq because we gon shrink
    j++; // shrink window on the left
    }
    max_length = max(max_length, i - j + 1);
    }
    return max_length;
    }
    };

  • @yongconglei1524
    @yongconglei1524 4 года назад +7

    There is some problem with the max_count. It's wrong to say the definition of it is the maximum count of same characters in the window. If it's, if the most frequent character get removed from the start, you should decrease the max_count.
    However, Nick didn't, meaning it should be the maximum count of same characters occurred so far. I don't know why this implementation can work. Maybe some explanation is missing? Can anyone explain it, expecially the definition of max_count?

    • @calfland79
      @calfland79 4 года назад +1

      max count is local. it is the max count for characters between start and end. Everytime start or end changes, max count changes accordingly.

    • @manojmarneni985
      @manojmarneni985 4 года назад

      Yeah it should be max_count_so_far, answer belongs the window where this occured. That is why this implementation works.
      If you consider another window w2 with max_same_char_count less than max_count_so_far, adding it with k will result in smaller substring.

    • @neeyatiajmera869
      @neeyatiajmera869 3 года назад +4

      We don't need to recalculate max_count because our goal is to find the substring where the max_count is maximum. The more the repeating characters in our string, the better ('aaaaab' is better than 'aab' because in the first one we have more repeating chars, therefore it's longer) . In the solution, when we move the start pointer forward and don't update the max_count, the substring gets counted as a valid substring (even if it isn't) but it doesn't matter to us because the length of this substring is smaller than the maxLength we found earlier. Try iterating through the second example test case step by step and you'll see for yourself as to why updating the max_count doesn't matter. Hope this helps!

    • @maxlau4321
      @maxlau4321 3 года назад

      @@neeyatiajmera869 wonderful!

    • @federicomadden9236
      @federicomadden9236 3 года назад

      @@neeyatiajmera869 How can it possibly be smaller than the maxlength we found earlier if the sliding window can only ever grow or stay the same size?

  • @sjy269
    @sjy269 3 года назад +4

    If you explain your code like you did in video, I wouldn't hire you.

    • @obinnaubah9045
      @obinnaubah9045 3 года назад

      Lol.

    • @apdr8783
      @apdr8783 3 года назад

      agreed, very sloppy, and impatient. he might be a good programmer if he actually knows these stuff not just memorizing it. but he would not be a good leader or mentor.

  • @subhedarsanchay
    @subhedarsanchay 4 года назад +7

    Got to recalculate max count after popping character from the front. Also, I am not sure what's the Big O order of this approach?

    • @itsbossable
      @itsbossable 4 года назад +2

      I had the same thought. But it is not required because if you look closely, size of the window never decreases. You can replace while with if and it will still work.

    • @PrasoonTelang
      @PrasoonTelang 4 года назад

      @@itsbossable thanks! Your comment helped me understand that.

    • @Cube2deth
      @Cube2deth 4 года назад

      @@PrasoonTelang I dont understand why?

    • @CinghAnkit
      @CinghAnkit 4 года назад +4

      @@Cube2deth i know its late. But replacing while with if will work because we are checking count of non repeating characters in every step and if count(of non repeating characters) >k then we immediately update start of window and k again k comes in range

    • @Cube2deth
      @Cube2deth 4 года назад +1

      @@CinghAnkit makes sense, thanks!

  • @sahastava75
    @sahastava75 11 месяцев назад +1

    Great video! Just one suggestion, you can replace the 'while' with 'if', it'll still work

  • @gulnurzehra
    @gulnurzehra 7 месяцев назад

    quick note, solution also works if you use an if statement instead of a while loop

  • @killeraloo3247
    @killeraloo3247 9 месяцев назад

    One of the best video about this question on the internet, I got it in 1 watch.
    Thanks for making it.
    🧡 from remote.

  • @shreshthsingh7744
    @shreshthsingh7744 3 года назад +1

    Sometimes I wonder why kevin and nick don't use whiteboard and pen to explain the solution.

  • @pacificmist09
    @pacificmist09 3 года назад

    nice video. thanks. one thing that took some time to get my head around was this maths. (window-end - window_start + 1 - max_count) > k.. I understood by reading it as (window_length - max_count) >k .. this is what tells us to adjust start of window because maxCount is no of repeated elements in the window and length - maxCount gives no of different elements in the window which are upper bound by k. so for example AAABCDE and k of 1, at window length 5 i.e. at element C, maxCount is 3 and hence no of different elements in window is 5-3 =2 which is > 1(k). hence we need to remove start.

  • @rohanshah6001
    @rohanshah6001 4 года назад +4

    Hi Nick,
    First of, excellent video!
    I was wondering why max_count keeps count of a character that is repeated the most, even though if its not a part of sliding window?
    Eg: s="aaaaabbcde"
    window_start = 1
    window_end = 7
    sliding window at this instance= "aaaabbc"
    As per our code, this will be a legal output (if we were to output string), right?
    So, I thought maybe max_count should be max frequency of any character within the sliding window i.e. 'a' occurs 4 times and so, max_count should be 4.
    The reason I am thinking this way is due to the fact that at each instance, we try to have a sliding window which accepts output.
    Thank you,
    Rohan

    • @aniketsharma5221
      @aniketsharma5221 4 года назад +3

      same doubt

    • @crazyhousepumela1939
      @crazyhousepumela1939 4 года назад

      if the new max count is introduced by the new idx, then with the current window it can not form a new valid substring with char at the new idx. if the max is still the same before this new idx then the max substring is covered by previous logic.

  • @sunilkumar2007
    @sunilkumar2007 4 года назад +7

    Hi Nick... This is a great video and I quite enjoyed the ending. That was cool.

  • @muditagarwal2658
    @muditagarwal2658 3 года назад +1

    The reason why he didn't have to update max_count in the while loop is because the while loop will run at most one iteration. So if that while was replaced with an "if", it still would work

    • @goodtoseeya1543
      @goodtoseeya1543 3 года назад +2

      Why use while loop though? the loop's not expected to run more than once, then doesn't it make more sense to just use "if"?

    • @muditagarwal2658
      @muditagarwal2658 3 года назад

      @@goodtoseeya1543 yes correct. try it out on leetcode.

    • @prateeksrivastava1577
      @prateeksrivastava1577 10 месяцев назад

      Precisely! I replaced 'while' with 'if' and the solution got accepted. Using 'while' was resulting in 'ArrayOutOfBound' exception.

  • @ianpan0102
    @ianpan0102 4 года назад +1

    don't understand the downvotes. I find your explanation intuitive.

  • @sagarprateek9216
    @sagarprateek9216 3 года назад

    People thinking (as I was) that why he is not updating the value of max_count if he is deleting an element from the window front which has that count is because the length of the window is never going to be greater than max_length until max_count is increased. Hence, no need of adding extra redundant steps.

    • @apdr8783
      @apdr8783 3 года назад

      But for instance if we have a string like ABACDE, once max_count reaches to 2 (due to we have 2 A), then the rest of the computation we are always using max_count = 2, that messes everyone up, isn't it??

  • @Madeinchinaagain
    @Madeinchinaagain 4 года назад +3

    What mic/audio capture settings do you use? It sounds like I'm wearing headphones when you speak

  • @saketsuman617
    @saketsuman617 4 года назад +1

    Hey Nick wondering if this could be solved using dp?
    Thinking aloud:
    If I do top down approach from i=0, then if I know the longest repeating character replacement (LRCR) for the substring starting from i+1th position then I could get the LRCR for substring starting from i under the constraint that if ith char is same as i+1th char or if the operations done are

  • @HimanshuKumar-ph8pj
    @HimanshuKumar-ph8pj 4 года назад +1

    nice and strong logic bro ...
    A little easier to understand solution would be to actually convert this probelm into max consecutive ones . where one(repeating character) can be changed to any character from A to Z .

  • @yygysgtyfugunvt
    @yygysgtyfugunvt 3 года назад

    Hey Nick I am from India,plzz don't feel frustrated you explain superbly

  • @nova2577
    @nova2577 4 года назад +4

    Like you the most among all who Leetcode on RUclips. Always intuitive, quick and crystal clear.

  • @rohandevaki4349
    @rohandevaki4349 2 года назад

    you said subarray problems are sliding window approach, then which problems do you suggest to solve to get these kind of appraoches? and thought process?

  • @ahmadsaeed7168
    @ahmadsaeed7168 4 года назад +4

    Jumped to code without explaining the algorithm. Waste of time.

  • @jinlin6308
    @jinlin6308 4 года назад +6

    I don't know why you feel frustrated, you explain it super clear, thanks

  • @amit0sunny
    @amit0sunny Год назад

    the inner while loop can be replaced with If condition.

  • @Karuska22ps
    @Karuska22ps 4 года назад +1

    how tf do you solve these during a coding challenge? I feel like most people just memorize these solutions smh

    • @EDROCKSWOO
      @EDROCKSWOO 3 года назад

      10 years will get you ready.

  • @jackedelic9188
    @jackedelic9188 3 года назад +1

    Hi Nick, 9:55 why not pause the recording so you have time to think or come up with examples? Or are you setting out to do the recording in one single shot?

  • @user-zk1nw9nu7w
    @user-zk1nw9nu7w Год назад

    I have doubt in while loop, why u added +1 >k?

  • @samrj444
    @samrj444 Год назад

    What's the brute force approach to this?

  • @nikhilsrivastava7719
    @nikhilsrivastava7719 Год назад

    if the frequency of the maximam count charecter decreases in the while loop the max_count should also decrease

  • @lifeofme3172
    @lifeofme3172 Год назад

    the + 1 is because we are using indexes and we need + 1 for actual length , i.e max length

  • @dkg4975
    @dkg4975 3 года назад

    For an input instance such as :- "AABC" and k = 2, what's the correct answer ? Will it be 3 or 4? Can we replace different characters at a time ? like can we replace both B and C as A or only A/ only B/ only C can be replaced ?

  • @pacificmist09
    @pacificmist09 3 года назад

    not sure if it works on ABCCDFC and k=1. answer is correct which is 3 but it does allow DFC in the sliding window

  • @jidongxiao2900
    @jidongxiao2900 2 года назад

    I think that while loop is unnecessary, you can just use if(){}, instead of while(){}.

  • @SuryanshSrivastava-de2lz
    @SuryanshSrivastava-de2lz Год назад

    Have you any ancestorial relationship with Walter White.

  • @goodguy6589
    @goodguy6589 Год назад

    As a beginner it look like..This guy is sick 😶

  • @samkitbarbhaya477
    @samkitbarbhaya477 Год назад

    This doesn't pass in Leetcode

  • @AD-fs6hr
    @AD-fs6hr 4 года назад +1

    Anyway I understand it after watching it twice. Would have been better with an extra example lol. Appreciate your work.

  • @reallifegambits
    @reallifegambits 4 года назад +1

    Loved your lazy side on 10:04 :D

  • @tl8035
    @tl8035 3 года назад

    why don't we have to update max_count in the while loop?

  • @ceprateek
    @ceprateek 2 года назад

    not sure about soo many thumbs down .. u explained it very well !! cheers

    • @ceprateek
      @ceprateek 2 года назад

      just to add algo is good too

  • @user-ht9cv4by9n
    @user-ht9cv4by9n Год назад

    You explained very well, man!

  • @manastripathi6013
    @manastripathi6013 3 года назад

    Why are we using max_count variable?

  • @AvinashS273
    @AvinashS273 4 года назад

    Audio balance in earphones is messed up

  • @darod6098
    @darod6098 4 года назад +1

    Good video man! Done is better than perfect hahaha, i prefer this video although obviusly with more examples could be a more clear explanation!

  • @_athie2544
    @_athie2544 3 года назад

    is the space complexity O(1) since the integer array is size of 26?

  • @willturner3440
    @willturner3440 3 года назад

    Code not working

  • @kevintravelsgram
    @kevintravelsgram 4 года назад

    Can someone explain why you don't have to adjust the max_count in the second while loop? Suppose that my maxCount is counting A and I remove A from the left index, why do I not need to decrement my maxCount?

    • @paulengland8983
      @paulengland8983 4 года назад +1

      Because it's the *max* and in the 2nd loop you're only decrementing. By definition it cannot go up.

    • @itsbossable
      @itsbossable 4 года назад

      I had the same thought. But it is not required because if you look closely, size of the window never decreases. You can replace while with if and it will still work.

    • @neeyatiajmera869
      @neeyatiajmera869 3 года назад

      We don't need to recalculate max_count because our goal is to find the substring where the max_count is maximum. The more the repeating characters in our string, the better ('aaaaab' is better than 'aab' because in the first one we have more repeating chars, therefore it's longer) . In the solution, when we move the start pointer forward and don't update the max_count, the substring gets counted as a valid substring (even if it isn't) but it doesn't matter to us because the length of this substring is smaller than the maxLength we found earlier. Try iterating through the second example test case step by step and you'll see for yourself as to why updating the max_count doesn't matter. Hope this helps!

  • @prashantjain5391
    @prashantjain5391 2 года назад

    Best Explanation Nick Thanks a lot :)

  • @sumitaggarwal1164
    @sumitaggarwal1164 5 месяцев назад

    best explanation

  • @emor0219
    @emor0219 3 года назад

    we love you too, nick!

  • @aanandroy5434
    @aanandroy5434 3 года назад

    I think you explained it well. thanks, man!

  • @arthurmorgan332
    @arthurmorgan332 3 года назад

    Thanks man, pretty good explanation.

  • @lifehacks9450
    @lifehacks9450 4 года назад +1

    u r really a nice guy

  • @gauravmishra8782
    @gauravmishra8782 7 месяцев назад

    wow explanation....

  • @ad6rsh
    @ad6rsh 4 года назад

    What's
    window_end - window_start - max_count + 1 > k. Can anyone explain that?

    • @alphaa458
      @alphaa458 4 года назад

      It's the number of characters in the window that need to flipped. Number of characters different from the majority character in the window.

    • @willturner3440
      @willturner3440 3 года назад

      Array 0 index based

  • @aman6561
    @aman6561 3 года назад

    great explanation.

  • @algorithmimplementer415
    @algorithmimplementer415 3 года назад

    A bit confusing explanation!

  • @shaikadam1828
    @shaikadam1828 4 месяца назад

    yo i got it

  • @weihua5305
    @weihua5305 4 года назад

    Thank you very much

  • @yacineboulaioune5401
    @yacineboulaioune5401 3 года назад

    This guy is great

  • @crack_code_interview
    @crack_code_interview 4 года назад

    Cool dude. Good explanation.

  • @philbowden
    @philbowden 2 месяца назад

    You are funny! Good video.

  • @suhasdara3040
    @suhasdara3040 4 года назад +2

    So condescending

  • @animeshsahu4126
    @animeshsahu4126 4 года назад

    " You know what I mean " that is your explanation bruuuh come on you can do better than that.

  • @gulnurzehra
    @gulnurzehra 7 месяцев назад

    quick note, solution also works if you use an if statement instead of a while loop