String Compression

Sdílet
Vložit
  • čas přidán 8. 07. 2024
  • For business inquiries email partnerships@k2.codes 10% OFF ROOFTOP SLUSHIE: bit.ly/kevinnaughton
    SOCIAL
    ----------------------------------------------------------------------------------------------------------------
    Instagram: / kevinnaughtonjr
    Twitter: / kevinnaughtonjr
    Patreon: / kevinnaughtonjr
    Merch: teespring.com/stores/kevin-na...
    GitHub: github.com/kdn251
    MUSIC
    ----------------------------------------------------------------------------------------------------------------
    xo bored llif3 by young frontwood
    / xo-bored-lif3
  • Věda a technologie

Komentáře • 118

  • @KevinNaughtonJr
    @KevinNaughtonJr  Před 4 lety +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

  • @aidanfarhi367
    @aidanfarhi367 Před 3 lety +1

    Very simple and straightforward. Thank you sir.

  • @NikashKumar
    @NikashKumar Před 4 lety

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

  • @rjose705
    @rjose705 Před 4 lety +50

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

  • @SameerKhan-vt1yo
    @SameerKhan-vt1yo Před 4 lety +2

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

  • @sandeepkumarverma8754
    @sandeepkumarverma8754 Před 4 lety

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

  • @kevintran7085
    @kevintran7085 Před 4 lety +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 Před 3 lety

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

    • @kevintran7085
      @kevintran7085 Před 3 lety

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

  • @srinish1993
    @srinish1993 Před 4 lety

    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.

  • @TK-zn5fb
    @TK-zn5fb Před 4 lety

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

  • @ankitagarwal4914
    @ankitagarwal4914 Před 4 lety

    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

  • @DonchenkoNadia
    @DonchenkoNadia Před 3 lety +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 Před 2 lety

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

  • @rajatdutta9870
    @rajatdutta9870 Před 4 lety

    Good explanation, keep up the great work.

  • @indiansoftwareengineer4899

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

  • @geetusharma3923
    @geetusharma3923 Před 4 lety +1

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

  • @rohitkadhe4521
    @rohitkadhe4521 Před 4 lety

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

  • @azamatik3
    @azamatik3 Před 11 měsíci

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

  • @alokmishra4468
    @alokmishra4468 Před 4 lety

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

  • @faraz7409
    @faraz7409 Před 4 lety +5

    thanks always great videos!!

  • @trishamalhotranayyar
    @trishamalhotranayyar Před 4 lety +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!

  • @jatinnandwani6678
    @jatinnandwani6678 Před 2 lety

    Thanks so much

  • @genghisda236
    @genghisda236 Před 4 lety +18

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

  • @FredWang
    @FredWang Před 4 lety

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

  • @GeraldoLopez
    @GeraldoLopez Před 3 lety

    hey man, thanks for making these videos.

  • @aditeeverma5676
    @aditeeverma5676 Před 3 lety +1

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

  • @algorithmimplementer415

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

  • @bhagyakasetti3991
    @bhagyakasetti3991 Před 2 lety

    what is the time complexity for this code?

  • @hestiaverof
    @hestiaverof Před 4 lety

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

  • @arvindmega
    @arvindmega Před 3 lety

    Awesome explanation

  • @shubhambhandari431
    @shubhambhandari431 Před 4 lety +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?

  • @devislive7710
    @devislive7710 Před 3 lety +1

    explaining the code logic on ipad is very good

  • @priyamshah7117
    @priyamshah7117 Před 2 lety

    very nicely explained

  • @deeppande
    @deeppande Před 4 lety

    Kevin, you rock !!!

  • @yolopassion
    @yolopassion Před 4 lety +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

  • @srivaishnav2319
    @srivaishnav2319 Před 3 lety +17

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

    • @KevinNaughtonJr
      @KevinNaughtonJr  Před 3 lety +7

      sri Vaishnav the beats are essential to interview prep

  • @miaohong601
    @miaohong601 Před 4 lety

    Hi Kevin, could you explain leetcode 73? Thanks.

  • @atift5465
    @atift5465 Před 4 lety +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 Před 2 lety

      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?

  • @AbhishekKumar-yv6ih
    @AbhishekKumar-yv6ih Před 4 lety +1

    You are awesome.
    Please do dp and backtracking questions.

  • @Meenereem
    @Meenereem Před 8 měsíci

    amazing thanks so much!

  • @itsahandle
    @itsahandle Před 4 lety +3

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

  • @pjamestx
    @pjamestx Před 4 lety +14

    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!

  • @Garensonic
    @Garensonic Před 3 lety

    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

  • @amansarma417
    @amansarma417 Před 4 lety

    Can you try this leetcode question
    Last Stone Weight II

  • @Hgh38
    @Hgh38 Před 4 lety +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 Před 4 lety

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

  • @leanobajio
    @leanobajio Před 4 lety +1

    Solid vid

  • @hollywoodedge
    @hollywoodedge Před rokem

    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 .

  • @Daemoniod
    @Daemoniod Před 4 lety +1

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

  • @ildar.ishalin.chelovek

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

  • @jatinnandwani6678
    @jatinnandwani6678 Před 2 lety

    Amazing

  • @srinish1993
    @srinish1993 Před 4 lety

    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.

  • @amitasaggi
    @amitasaggi Před 4 lety

    does this solution work? It does not work for me

  • @miaohong601
    @miaohong601 Před 4 lety

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

  • @charlesbickham6604
    @charlesbickham6604 Před 4 lety +32

    Please keep doin the ipad !

  • @MrMakemusicmike
    @MrMakemusicmike Před 3 lety

    that is damn good

  • @apocvlypse
    @apocvlypse Před 3 lety +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.

  • @meyrlansuirkhanov1358
    @meyrlansuirkhanov1358 Před 4 lety

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

  • @brandyplays6134
    @brandyplays6134 Před 4 lety

    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  Před 4 lety

      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 Před 4 lety

      @@KevinNaughtonJr thanks alot will have a look:)

    • @brandyplays6134
      @brandyplays6134 Před 4 lety

      @@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 Před 3 lety

      @@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

  • @fabtjar
    @fabtjar Před rokem

    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.

  • @enkr1
    @enkr1 Před rokem

    well explained.

  • @prakyathkini5540
    @prakyathkini5540 Před 2 lety

    king

  • @sujeeshsvalath
    @sujeeshsvalath Před 4 lety

    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.

  • @ConernicusRex
    @ConernicusRex Před 6 měsíci

    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

  • @cloud5887
    @cloud5887 Před 4 lety

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

  • @rohitdatta5029
    @rohitdatta5029 Před 3 lety

    classic

  • @raulbarriga7416
    @raulbarriga7416 Před 3 lety

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

  • @yolopassion
    @yolopassion Před 4 lety +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  Před 4 lety

      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 Před 4 lety

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

    • @yolopassion
      @yolopassion Před 4 lety

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

  • @hemantsood9579
    @hemantsood9579 Před 4 lety

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

  • @rutujakaushike8759
    @rutujakaushike8759 Před 4 lety

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

  • @dragonore2009
    @dragonore2009 Před 2 lety

    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.

  • @faridkhan1954
    @faridkhan1954 Před 3 lety

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

  • @jasoncase6802
    @jasoncase6802 Před 3 lety +1

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

    • @MrMakemusicmike
      @MrMakemusicmike Před 3 lety

      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.

  • @prathamkesarkar
    @prathamkesarkar Před 4 lety

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

  • @pavanpalleti8580
    @pavanpalleti8580 Před 4 lety

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

  • @moeal5110
    @moeal5110 Před 2 lety

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

  • @MrAnshul1992
    @MrAnshul1992 Před 2 lety

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

  • @oscaropdyou
    @oscaropdyou Před 4 lety

    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

  • @saiKiran-ps8ff
    @saiKiran-ps8ff Před rokem

    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

  • @vivekkeshri536
    @vivekkeshri536 Před 4 lety

    the array output is a3b2c3cc

    • @vivekkeshri536
      @vivekkeshri536 Před 4 lety

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

  • @ConernicusRex
    @ConernicusRex Před 6 měsíci

    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.

  • @KalatitKalyan
    @KalatitKalyan Před rokem

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

  • @subhambanerjee6334
    @subhambanerjee6334 Před 4 lety +1

    iPad was helpful again

  • @dhwajsharma
    @dhwajsharma Před 3 lety +1

    Dislikes 2040 lol

  • @fetullahatas3927
    @fetullahatas3927 Před 4 lety

    can you shut the background music down pls , very annoying

  • @johnkim2161
    @johnkim2161 Před 3 lety

    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 Před 3 lety +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 Před 2 lety

    Amazing