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
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!
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!
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!
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?
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.
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.
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?
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.
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
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
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
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
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.
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.
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).
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:)
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/
@@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.
@@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
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.
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.
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
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?
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; }
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; }
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
I like this Sponsor.
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
Just a note: Reassigning within a function will create a local variable to the function, and won't modify the argument array.
Do the logical whiteboard thing more often i learn more from it. Also you are best !
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!
Dud could you point out what else you used for your preparation?
@@kewtomrao LeetCode. Get the premium option where you can use a debugger.
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!
Ofcourse we still love the iPad. And since u seriously told us to follow , so i followed u on instagram.
LOVE IT
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!
this.
Why I watch your videos.....
42% for Interview preperation.
58% for Your Intro music.
sri Vaishnav the beats are essential to interview prep
Please keep doin the ipad !
yes... iPad would be helpful for many problems.
yeah kevin use the ipad often
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?
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.
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.
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?
Very simple and straightforward. Thank you sir.
I think the Ipad is very helpful Kevin! As always I appreciate your explanations :)
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.
When Kevin's lamps aren't burning
>Safety Secured
Oh they’ll be back don’t worry
@@KevinNaughtonJr Are you from Denmark....... *Hygge* ??
sri Vaishnav nah NYC but I loooove Denmark why do you ask?
explaining the code logic on ipad is very good
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
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
This is one of coolest videos of yours,. Thanks mate
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.
thank you very much, at first this task seemed unsolvable, but with your explanation it became much easier
thanks always great videos!!
anytime Faraz!
wow, this ipad's idea is awesome, than previous ones.
loved this channel.
Great work .. thank you so much . 🌠❤️
Can you please discuss, how to think about edge cases in these questions?
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
Like the way you explain the problem statement and the approach to solve it :-)
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 .
Thanks so much
I'm in love with your channels, keep'em coming
Why you could not have it done in a single loop and a single string object as extra space?
O(n) time complexity.
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
wait how does the math work exactly for the j-i, im a little confused on that
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.
Hello, why do we need index ? Index and I always have the same value right ?
Hi Kevin, great video! Love the new style! Could you do longest palindromic substring?
What’s the app on iPad you are using for demonstrating this problem? Thanks.
Why do you modify chars? you could save time with just index++
You are awesome.
Please do dp and backtracking questions.
Highly appreciate your work. Could you solve the sorted elements in a rotated array problem?
Can you please share some resources of Recursion and who to approach backtracking problems.
This solution DOES NOT work for the input ["a","b","b","b","b","b","b","b","b","b","b","b","b"].
very nicely explained
what is the time complexity for this code?
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.
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.
Your code is wrong Sir
class Solution {
public int compress(char[] chars) {
int index=0;
int i=0;
while(i
Could you do the video on this Count Complete Tree Nodes (Leetcode 222), the binary search version?
Awesome explanation
does this solution work? It does not work for me
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).
because i is set equal to j after we finish the inner loop, so you only ever visit each character once
Good explanation, keep up the great work.
Ipad is helpful :) Helps show the logic to me in a human way
Hi Kevin, could you explain leetcode 73? Thanks.
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:)
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/
@@KevinNaughtonJr thanks alot will have a look:)
@@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.
@@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
well explained.
hey man, thanks for making these videos.
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.
IBM just asked me this question via HackerRank for a Front End position.
Kevin, you rock !!!
Can you try this leetcode question
Last Stone Weight II
amazing thanks so much!
How the hell this is an easy questions? Are the questions getting harder and harder for the interviews?
Solid vid
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.
Hi Kevin, I really appreciate your videos. Could you also make a video for leetcode 331. Verify Preorder Serialization of a Binary Tree? Thanks
(python)getting [] as output:-
class Solution(object):
def compress(self, chars):
"""
:type chars: List[str]
:rtype: int
"""
i=0
index=0
while i
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
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
king
Nice video. Can u plz make more videos about interview for IT companies?)
On LC it says it’s a low frequent question asked by amazon. Where you got that from?
Amazing
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?
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
@@KevinNaughtonJr Thank you so much I will check this out
I have checked this but I am not sure how this thing works
@Kevin Leetcode 568. Maximum Vacation Days can you please solve this
the array output is a3b2c3cc
wel the expected is a3b2c3 please check the code and change the logic
that is damn good
Video for LC 99. recover binary search tree please! TIA
iPad was helpful again
Dislikes 2040 lol
Hey please upload the solution on Leetcode #424. Longest Repeating Character Replacement.
i am waiting for solution
classic
Bro said pointer while coding in java, huh (just a lame joke)
can you shut the background music down pls , very annoying
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;
}
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;
}
Amazing