String Compression

แชร์
ฝัง
  • เผยแพร่เมื่อ 11 ก.ย. 2024

ความคิดเห็น • 119

  • @KevinNaughtonJr
    @KevinNaughtonJr  4 ปีที่แล้ว +7

    10% OFF ROOFTOP SLUSHIE: bit.ly/kevinnaughton
    instagram: instagram.com/kevinnaughtonjr/
    twitter: twitter.com/kevinnaughtonjr
    patreon: www.patreon.com/KevinNaughtonJr
    merch: teespring.com/stores/kevin-naughton-jrs-store

    • @jlecampana
      @jlecampana 4 ปีที่แล้ว

      I like this Sponsor.

  • @DonchenkoNadia
    @DonchenkoNadia 3 ปีที่แล้ว +8

    Thank you, Kevin!
    The explanation is great! ❤️
    My Python version below:
    class Solution:
    def compress(self, chars: List[str]) -> int:
    i = 0
    to = 0
    while i < len(chars):
    j = i
    while j < len(chars) and chars[j] == chars[i]:
    j += 1
    num = j - i
    chars[to] = chars[i]
    to += 1
    if num > 1:
    for digit in str(num):
    chars[to] = digit
    to += 1
    i = j
    chars = chars[:to]
    return to

    • @sketch1625
      @sketch1625 2 ปีที่แล้ว

      Just a note: Reassigning within a function will create a local variable to the function, and won't modify the argument array.

  • @rjose705
    @rjose705 4 ปีที่แล้ว +50

    Do the logical whiteboard thing more often i learn more from it. Also you are best !

  • @kevintran7085
    @kevintran7085 4 ปีที่แล้ว +19

    Hey Kevin, I watched all of your LeetCoding videos to help me prepare for my technical interviews. I was able to secure two offers from big tech companies (including a FAANG). I just wanted to thank you for making these videos and helping people like myself achieve their professional goals!

    • @kewtomrao
      @kewtomrao 3 ปีที่แล้ว

      Dud could you point out what else you used for your preparation?

    • @kevintran7085
      @kevintran7085 3 ปีที่แล้ว

      @@kewtomrao LeetCode. Get the premium option where you can use a debugger.

  • @trishamalhotranayyar
    @trishamalhotranayyar 4 ปีที่แล้ว +4

    I personally didn't need the white board explanation and found myself pressing fast forward multiple times to reach the code while ensuring I didn't miss anything on the whiteboard. Commenting to be a feedback data point for your decision. Thanks for your efforts!

  • @genghisda236
    @genghisda236 4 ปีที่แล้ว +18

    Ofcourse we still love the iPad. And since u seriously told us to follow , so i followed u on instagram.

  • @pjamestx
    @pjamestx 4 ปีที่แล้ว +15

    Instead of using two pointers, another option would be a variable to keep track of what character you're on, and one to keep the count of the number of times you've seen it. That way you avoid the appearance of nested for loops, and it's possibly easier to read (although that's always a matter of opinion). Thanks for all the videos, they're really helping me prep for interviews!

    • @rjose705
      @rjose705 4 ปีที่แล้ว

      this.

  • @srivaishnav2319
    @srivaishnav2319 4 ปีที่แล้ว +17

    Why I watch your videos.....
    42% for Interview preperation.
    58% for Your Intro music.

    • @KevinNaughtonJr
      @KevinNaughtonJr  4 ปีที่แล้ว +7

      sri Vaishnav the beats are essential to interview prep

  • @charlesbickham6604
    @charlesbickham6604 4 ปีที่แล้ว +32

    Please keep doin the ipad !

    • @aamirjamal6833
      @aamirjamal6833 4 ปีที่แล้ว

      yes... iPad would be helpful for many problems.

    • @anonymousvoid6356
      @anonymousvoid6356 2 ปีที่แล้ว

      yeah kevin use the ipad often

  • @shubhambhandari431
    @shubhambhandari431 4 ปีที่แล้ว +2

    Ip - abbbbbbbbbb. Length is 13
    Op- a b 1 2 length is 4
    Using same array to compress..
    I am little confusd. Where is deleting code of that array's extra space .
    Char array had 13 length but after compression char array's length should be 4 right?

  • @apocvlypse
    @apocvlypse 3 ปีที่แล้ว +1

    This solution modifies the array, but it does NOT compress it. Before returning index you'll need to erase all elements position index to the end. Using the example chars = ["a","a","b","b","c","c","c"], your modified array will be ["a","2","b","2","c","3","c"] unless you erase the elements after position index. I don't think leetcode actually checks the modified array.

  • @atift5465
    @atift5465 4 ปีที่แล้ว +3

    I really like how you use the whiteboard from the Ipad to draw out the explanations! Also, this problem really threw me off when i did it. I used the HashmMap with counter value implementation and never knew an edge case where if the same letter occurs after certain other letters - it would need its own separate count. My hashmap implementation just had the total count of same keys obviously so the algorithm was wrong. Leetcode never put that edge case in its example because it would completely change my algorithm.

    • @ivymboya
      @ivymboya 2 ปีที่แล้ว

      This is my current implementation using python's counter object. I don't get it when you say that if the same letter occurs after certain other letters, it needs its own separate count. Could you explain?

  • @aidanfarhi367
    @aidanfarhi367 3 ปีที่แล้ว +1

    Very simple and straightforward. Thank you sir.

  • @SameerKhan-vt1yo
    @SameerKhan-vt1yo 4 ปีที่แล้ว +2

    I think the Ipad is very helpful Kevin! As always I appreciate your explanations :)

  • @srinish1993
    @srinish1993 4 ปีที่แล้ว

    I usually keep the slow pointer above the array and the faster pointer below the array while making a dry run through the array.
    I hope this helps in ur white-board representations.
    Thank you sharing solution for this question again.

  • @itsahandle
    @itsahandle 4 ปีที่แล้ว +3

    When Kevin's lamps aren't burning
    >Safety Secured

    • @KevinNaughtonJr
      @KevinNaughtonJr  4 ปีที่แล้ว +2

      Oh they’ll be back don’t worry

    • @srivaishnav2319
      @srivaishnav2319 4 ปีที่แล้ว

      @@KevinNaughtonJr Are you from Denmark....... *Hygge* ??

    • @KevinNaughtonJr
      @KevinNaughtonJr  4 ปีที่แล้ว +2

      sri Vaishnav nah NYC but I loooove Denmark why do you ask?

  • @devislive7710
    @devislive7710 4 ปีที่แล้ว +1

    explaining the code logic on ipad is very good

  • @yolopassion
    @yolopassion 4 ปีที่แล้ว +4

    Huff !! I finally completed your leetcode playlist today and it feels so great.I don't know how much I will be able to remember but please wish me luck for my forthcoming interview

  • @ankitagarwal4914
    @ankitagarwal4914 4 ปีที่แล้ว

    Your solutions to problems are very refined. One can trust and focus on your approach ...90% on the solutions on leetcode and other channels doesnot have high standards.. I have referred you channel to all my friends

  • @NikashKumar
    @NikashKumar 4 ปีที่แล้ว

    This is one of coolest videos of yours,. Thanks mate

  • @fabtjar
    @fabtjar ปีที่แล้ว

    I don't get the point of chars[index++] = chars[i]. Why not just increase index? I don't see the point of changing the array.

  • @azamatik3
    @azamatik3 ปีที่แล้ว

    thank you very much, at first this task seemed unsolvable, but with your explanation it became much easier

  • @faraz7409
    @faraz7409 4 ปีที่แล้ว +5

    thanks always great videos!!

  • @indiansoftwareengineer4899
    @indiansoftwareengineer4899 3 ปีที่แล้ว

    wow, this ipad's idea is awesome, than previous ones.
    loved this channel.

  • @geetusharma3923
    @geetusharma3923 4 ปีที่แล้ว +1

    Great work .. thank you so much . 🌠❤️
    Can you please discuss, how to think about edge cases in these questions?

  • @ConernicusRex
    @ConernicusRex 8 หลายเดือนก่อน

    solution following the above method in Swift:
    class Solution {
    func compress(_ chars: inout [Character]) -> Int {
    let lastIndex = chars.count - 1
    var index = 0
    var i = 0
    while i

  • @sandeepkumarverma8754
    @sandeepkumarverma8754 4 ปีที่แล้ว

    Like the way you explain the problem statement and the approach to solve it :-)

  • @hollywoodedge
    @hollywoodedge ปีที่แล้ว

    This code won't work for the test case aaaaabbbbbbacccc I put an extra a there and this code won't work . We need either a hash map or an array .

  • @jatinnandwani6678
    @jatinnandwani6678 2 ปีที่แล้ว

    Thanks so much

  • @aditeeverma5676
    @aditeeverma5676 4 ปีที่แล้ว +1

    I'm in love with your channels, keep'em coming

  • @faridkhan1954
    @faridkhan1954 3 ปีที่แล้ว

    Why you could not have it done in a single loop and a single string object as extra space?
    O(n) time complexity.

  • @Garensonic
    @Garensonic 3 ปีที่แล้ว

    I have a Google phone screen next week and I am very scared. I had a Google phone screen five years ago for a new grad position and did not get past the phone screen so I am scared

  • @jasoncase6802
    @jasoncase6802 3 ปีที่แล้ว +1

    wait how does the math work exactly for the j-i, im a little confused on that

    • @MrMakemusicmike
      @MrMakemusicmike 3 ปีที่แล้ว

      as you enter the 2nd loop, j is incremented by 1, the difference between j and I is how you moved over similar chars (bbb) = 3.

  • @hestiaverof
    @hestiaverof 4 ปีที่แล้ว

    Hello, why do we need index ? Index and I always have the same value right ?

  • @rohitkadhe4521
    @rohitkadhe4521 4 ปีที่แล้ว

    Hi Kevin, great video! Love the new style! Could you do longest palindromic substring?

  • @FredWang
    @FredWang 4 ปีที่แล้ว

    What’s the app on iPad you are using for demonstrating this problem? Thanks.

  • @ildar.ishalin.chelovek
    @ildar.ishalin.chelovek 4 ปีที่แล้ว

    Why do you modify chars? you could save time with just index++

  • @AbhishekKumar-yv6ih
    @AbhishekKumar-yv6ih 4 ปีที่แล้ว +1

    You are awesome.
    Please do dp and backtracking questions.

  • @TK-zn5fb
    @TK-zn5fb 4 ปีที่แล้ว

    Highly appreciate your work. Could you solve the sorted elements in a rotated array problem?

  • @alokmishra4468
    @alokmishra4468 4 ปีที่แล้ว

    Can you please share some resources of Recursion and who to approach backtracking problems.

  • @pavanpalleti8580
    @pavanpalleti8580 4 ปีที่แล้ว

    This solution DOES NOT work for the input ["a","b","b","b","b","b","b","b","b","b","b","b","b"].

  • @priyamshah7117
    @priyamshah7117 2 ปีที่แล้ว

    very nicely explained

  • @bhagyakasetti3991
    @bhagyakasetti3991 2 ปีที่แล้ว

    what is the time complexity for this code?

  • @ConernicusRex
    @ConernicusRex 8 หลายเดือนก่อน

    You first minute is a little deceptive. Yes it's harder to get into a FAANG company than to get into Harvard but not because it's hard to land an interview. It's because that interview process is grueling and forces any applicant to prove they have the knowledge base to do the job they're applying for. It has a low acceptance rate for applicants, not a low number of interviews. In fact, Alphabet receives over 25 million applications in a year for engineering positions worldwide and conducts well over 10 million interview series. Meaning well over 35% of applicants are granted an interview. It's not hard to land one, it's hard to get to stage 2, 3, 4, 5, and an offer.

  • @dragonore2009
    @dragonore2009 3 ปีที่แล้ว

    Leetcode fails again in giving good examples. Yes it could be "aaabbcc" or "abbbbbb" or "a", but it could also be "aaabbcccca" which would be 3a2b4c1a => [3, a, 2, b, 4, c, a]. It's a shame leetcode lets us down yet again on poor examples.

  • @Chaloobolo
    @Chaloobolo ปีที่แล้ว

    Your code is wrong Sir
    class Solution {
    public int compress(char[] chars) {
    int index=0;
    int i=0;
    while(i

  • @algorithmimplementer415
    @algorithmimplementer415 4 ปีที่แล้ว

    Could you do the video on this Count Complete Tree Nodes (Leetcode 222), the binary search version?

  • @arvindmega
    @arvindmega 3 ปีที่แล้ว

    Awesome explanation

  • @amitasaggi
    @amitasaggi 4 ปีที่แล้ว

    does this solution work? It does not work for me

  • @Hgh38
    @Hgh38 4 ปีที่แล้ว +1

    Could anyone explain how come the counting the number of characters in j - I doesn’t affect the runtime? Like would in the worst case wouldn’t it be O(n^2).

    • @trevoratlas
      @trevoratlas 4 ปีที่แล้ว

      because i is set equal to j after we finish the inner loop, so you only ever visit each character once

  • @rajatdutta9870
    @rajatdutta9870 4 ปีที่แล้ว

    Good explanation, keep up the great work.

  • @Daemoniod
    @Daemoniod 4 ปีที่แล้ว +1

    Ipad is helpful :) Helps show the logic to me in a human way

  • @miaohong601
    @miaohong601 4 ปีที่แล้ว

    Hi Kevin, could you explain leetcode 73? Thanks.

  • @brandyplays6134
    @brandyplays6134 4 ปีที่แล้ว

    wow this is so amazing the way you explained..thanks alot..kevin wanted to ask you something..can u make a separate playlist on problems on only linked list then only stacks etc covering all the data structures..it would really help..thanks in advance:)

    • @KevinNaughtonJr
      @KevinNaughtonJr  4 ปีที่แล้ว

      Brandyplays if that’s what you’re looking for you should check out the interviewing service I launched, The Daily Byte! I recommend the annual subscription: thedailybyte.dev/

    • @brandyplays6134
      @brandyplays6134 4 ปีที่แล้ว

      @@KevinNaughtonJr thanks alot will have a look:)

    • @brandyplays6134
      @brandyplays6134 4 ปีที่แล้ว

      @@KevinNaughtonJr hey kevin can you do a problem like this..it would really help..thanks in advance:)
      Alternate Lower Upper String Sort
      Example:
      Input: bAwutndekWEdkd
      Output: AbEdWddekkntuw
      Explanation:
      Here we can see that letter ‘A’,’E’,’W’ are sorted as well as letters “b,d,d,d,e,k,k,n,t,u,w” are sorted but both appears alternately in the string as far as possible.

    • @srivaishnav2319
      @srivaishnav2319 4 ปีที่แล้ว

      @@brandyplays6134 Hash all capitals in one hash map, and all smalls in another one. Then let char cap = 'A' and char small = 'a' and i= cap. then while both has maps are not empty and cap

  • @enkr1
    @enkr1 ปีที่แล้ว

    well explained.

  • @GeraldoLopez
    @GeraldoLopez 4 ปีที่แล้ว

    hey man, thanks for making these videos.

  • @sujeeshsvalath
    @sujeeshsvalath 4 ปีที่แล้ว

    Hi Kevin, I came across an interesting question: its input is "abcacdcef", then output should be ["abcacdc", "ef"], ie between 'a' and next occurrence of 'a', there is 'b' and 'c'. So a,b,c forms a connected component. So the component expanded to "abcacdc" and since "ef" has no connected component, its consider as it is . So ultimately it is broken down into ["abcacdc", "ef"]. Getting no clue how to solve this. Can you please share link if you have came across similar type.

  • @raulbarriga7416
    @raulbarriga7416 3 ปีที่แล้ว

    IBM just asked me this question via HackerRank for a Front End position.

  • @deeppande
    @deeppande 4 ปีที่แล้ว

    Kevin, you rock !!!

  • @amansarma417
    @amansarma417 4 ปีที่แล้ว

    Can you try this leetcode question
    Last Stone Weight II

  • @Meenereem
    @Meenereem 10 หลายเดือนก่อน

    amazing thanks so much!

  • @moeal5110
    @moeal5110 2 ปีที่แล้ว

    How the hell this is an easy questions? Are the questions getting harder and harder for the interviews?

  • @leanobajio
    @leanobajio 4 ปีที่แล้ว +1

    Solid vid

  • @srinish1993
    @srinish1993 4 ปีที่แล้ว

    I am thinking of buying subscription of ROOFTOP SLUSHIE. But I needed to know how effective is ROOFTOP SLUSHIE if we are looking to appear for interviews at india-office based FAANG companies. As many of you know due to COVID situation recruiters aren't responding as they used to earlier, either on linkeding or job-portals. Can anyone share experience of ROOFTOP SLUSHIE in india.

  • @miaohong601
    @miaohong601 4 ปีที่แล้ว

    Hi Kevin, I really appreciate your videos. Could you also make a video for leetcode 331. Verify Preorder Serialization of a Binary Tree? Thanks

  • @MrAnshul1992
    @MrAnshul1992 2 ปีที่แล้ว

    (python)getting [] as output:-
    class Solution(object):
    def compress(self, chars):
    """
    :type chars: List[str]
    :rtype: int
    """
    i=0
    index=0
    while i

  • @saiKiran-ps8ff
    @saiKiran-ps8ff ปีที่แล้ว

    import json
    class Solution:
    def compress(self, chars: List[str]) -> List[str]:
    l = len(chars)
    res = chars[0]
    temp = chars[0]
    c = 1
    for i in chars[1:]:
    if i == temp:
    c += 1
    else:
    if c > 1:
    res += str(c)
    c = 1
    temp = i
    res += i
    if c > 1:
    res += str(c)
    mod_res = list(res)
    mod1_res = json.dumps(mod_res)
    return

    what i should return here

  • @oscaropdyou
    @oscaropdyou 4 ปีที่แล้ว

    Improvised your solution -
    public int compress(char[] chars) {
    int write = 0;
    int currentCount = 1;
    char currentChar = '0';
    String countString;
    for(int i=0; i< chars.length; i++) {
    currentChar = chars[i];
    if(i == chars.length -1 || currentChar != chars[i+1]) {
    chars[write++] = currentChar;
    if(currentCount > 1) {
    countString = String.valueOf(currentCount);
    for(int j=0; j

  • @prakyathkini5540
    @prakyathkini5540 2 ปีที่แล้ว

    king

  • @meyrlansuirkhanov1358
    @meyrlansuirkhanov1358 4 ปีที่แล้ว

    Nice video. Can u plz make more videos about interview for IT companies?)

  • @cloud5887
    @cloud5887 4 ปีที่แล้ว

    On LC it says it’s a low frequent question asked by amazon. Where you got that from?

  • @jatinnandwani6678
    @jatinnandwani6678 2 ปีที่แล้ว

    Amazing

  • @yolopassion
    @yolopassion 4 ปีที่แล้ว +1

    Kevin Naughton Jr. I have an upcoming Google interview in a few days so I have been following your leet code interview questions playlist lately to go through the first round. I am hell lot of scared as I am nothing compared to you when it terms to coding. Can you suggest anything that I should do to prepare for it?

    • @KevinNaughtonJr
      @KevinNaughtonJr  4 ปีที่แล้ว

      Check out my Patreon page I offer tons of services to help people make sure they're prepared for their interviews! www.patreon.com/KevinNaughtonJr

    • @yolopassion
      @yolopassion 4 ปีที่แล้ว

      @@KevinNaughtonJr Thank you so much I will check this out

    • @yolopassion
      @yolopassion 4 ปีที่แล้ว

      I have checked this but I am not sure how this thing works

  • @prathamkesarkar
    @prathamkesarkar 4 ปีที่แล้ว

    @Kevin Leetcode 568. Maximum Vacation Days can you please solve this

  • @vivekkeshri536
    @vivekkeshri536 4 ปีที่แล้ว

    the array output is a3b2c3cc

    • @vivekkeshri536
      @vivekkeshri536 4 ปีที่แล้ว

      wel the expected is a3b2c3 please check the code and change the logic

  • @MrMakemusicmike
    @MrMakemusicmike 3 ปีที่แล้ว

    that is damn good

  • @rutujakaushike8759
    @rutujakaushike8759 4 ปีที่แล้ว

    Video for LC 99. recover binary search tree please! TIA

  • @subhambanerjee6334
    @subhambanerjee6334 4 ปีที่แล้ว +1

    iPad was helpful again

  • @dhwajsharma
    @dhwajsharma 3 ปีที่แล้ว +1

    Dislikes 2040 lol

  • @hemantsood9579
    @hemantsood9579 4 ปีที่แล้ว

    Hey please upload the solution on Leetcode #424. Longest Repeating Character Replacement.
    i am waiting for solution

  • @rohitdatta5029
    @rohitdatta5029 3 ปีที่แล้ว

    classic

  • @programmingpython584
    @programmingpython584 หลายเดือนก่อน

    Bro said pointer while coding in java, huh (just a lame joke)

  • @fetullahatas3927
    @fetullahatas3927 4 ปีที่แล้ว

    can you shut the background music down pls , very annoying

  • @johnkim2161
    @johnkim2161 3 ปีที่แล้ว

    Hey Kevin,
    First, really love your channel, very useful for the coding assessment preparing.
    Today I did the amazon hackerrank code assessment, and got stuck with one question of Transaction logs. It is not really hard to understand, but for 15 testing cases, I only able to pass 7 of them. I am just wondering can you take a look of my code here, to see where I did wrong, could improve it? Really appreciated.
    //Time: O(n(logn)) -- due to the sort
    //Space: O(n)
    internal List ProcessLog(List logs, int threshold)
    {
    var map = new Dictionary();
    var retValue = new List();
    foreach(var log in logs)
    {
    var temp = log.Split(' ');
    if(temp[1] != temp[0])
    {
    map[temp[0]] = map.ContainsKey(temp[0]) ? map[temp[0]] +1 : 1;
    map[temp[1]] = map.ContainsKey(temp[1]) ? map[temp[1]] + 1 : 1;
    }
    }
    foreach(var entry in map)
    {
    if (entry.Value >= threshold)
    retValue.Add(entry.Key);
    }
    retValue.Sort();
    return retValue;
    }

  • @johnkim2161
    @johnkim2161 3 ปีที่แล้ว +1

    Hey Kevin,
    First want to say, really enjoying your channel, and it really helpful for interview prepare.
    Today, I did an online-assessment with amazon, and I stuck at this question called Transaction Logs.
    I say it is not difficult one, but I just couldn't pass all the cases.
    Here is my code, could you take a look and help me improve it? Really appreciated here.
    Sorry, I wrote it C#, but it wouldn't too big different than JAVA.
    //Time: O(n(logn)) -- due to the sort
    //Space: O(n)
    internal List ProcessLog(List logs, int threshold)
    {
    var map = new Dictionary();
    var retValue = new List();
    foreach(var log in logs)
    {
    var temp = log.Split(' ');
    if(temp[1] != temp[0])
    {
    map[temp[0]] = map.ContainsKey(temp[0]) ? map[temp[0]] +1 : 1;
    map[temp[1]] = map.ContainsKey(temp[1]) ? map[temp[1]] + 1 : 1;
    }
    }
    foreach(var entry in map)
    {
    if (entry.Value >= threshold)
    retValue.Add(entry.Key);
    }
    retValue.Sort();
    return retValue;
    }

  • @jatinnandwani6678
    @jatinnandwani6678 2 ปีที่แล้ว

    Amazing