Interview Question: String Compression

  Рет қаралды 42,624

Byte by Byte

Byte by Byte

Күн бұрын

Пікірлер: 63
@martincopes3818
@martincopes3818 8 жыл бұрын
13:55 This algorithm does not run in linear time, it actually has a quadratic runtime complexity. Since Strings in Java are immutable, concatenation using the + operator takes linear time because it has to copy the whole string. A more efficient solution would be using the StringBuilder class to build the output. Appending to a StringBuilder has an amortized runtime of O(1), thus achieving an overall linear complexity.
@ByteByByte
@ByteByByte 8 жыл бұрын
Yes I should have specified that a string builder would be better. Generally it's not necessary to actually use a string builder in your interview solution as long as you make it clear to you interviewer that that is what you would do in production code. I find string builders overcomplicate interview solutions with no real benefit
@Grassmpl
@Grassmpl 7 жыл бұрын
or if your not familiar with string builder, you can use java.util.LinkedList out, then return new String(out);
@srivastava_prateek
@srivastava_prateek 7 жыл бұрын
Same was i thinking
@BRUCE59468
@BRUCE59468 5 жыл бұрын
Yes but most compilers will optimize this.
@baichen6449
@baichen6449 5 жыл бұрын
if I use a stringbuilder what is the space complexity? will it be O(n)?
@josiegeng7969
@josiegeng7969 6 жыл бұрын
Very nice and clear explanation! Can you pls do an in-place version of it? Using O(1) space.
@ByteByByte
@ByteByByte 6 жыл бұрын
Not really sure what you're asking. There's no such thing as an in place string modification in java because strings are immutable
@fredianriko5648
@fredianriko5648 3 жыл бұрын
Hey thanks for the solution discussions, i got the same question but with added complexity, which the questions have a possibility to generate input of combination with of different characters, such as “abcabcancdddcadb” the question expected to return “2abcancdddcadb” , or “abcabcanc3dadb”, also the questions is required for me to return the shortest compressed string possible, which trifling me by the complexity. By any chance could you please discuss it? Thanks
@angelurmasekur
@angelurmasekur 4 жыл бұрын
Is this a more efficient solution than maintaining a 256 int array, where each index represents the unicode value of a char. Running a for loop over the initial string and incrementing the int count at each char's index value. Then a separate for loop over the array, where if int > 0, get the char value of the index and the value at that position and append it to a stringBuilder/string? Then in the end, do the same ternary statement you have to return the shorter string
@ramyaby977
@ramyaby977 3 жыл бұрын
Great solution, thank you so much
@fxsabreu
@fxsabreu 5 жыл бұрын
Nice video, nice series, nice explanation. Keep up the good work!
@omnnnooy3267
@omnnnooy3267 6 жыл бұрын
Amazing series!! Thank you so much
@ByteByByte
@ByteByByte 6 жыл бұрын
Thanks!
@tanzeelrana7348
@tanzeelrana7348 3 жыл бұрын
What about string "abb" ?
@praneethaluru2601
@praneethaluru2601 6 жыл бұрын
Good point mentioned at 5:49
@fetchjim
@fetchjim 8 жыл бұрын
If the input string s is empty the line "out = out + s.charAt(s.length() - 1) + sum" will try to reference the character of s at index -1, which will result in an IndexOutOfBoundsException. There could be a check at the beginning of the function to return "" if s is empty.
@ByteByByte
@ByteByByte 8 жыл бұрын
yeah that's probably the simplest fix
@jit3191
@jit3191 8 жыл бұрын
I like the simple explanation.thanks
@ByteByByte
@ByteByByte 8 жыл бұрын
thanks :)
@brinderdhaliwal3570
@brinderdhaliwal3570 7 жыл бұрын
Thanks for the clear solution. First time working through CTCI, and after working with c++, (all I've taken in my classes thus far) I was surprised that I could totally follow along with your Java solution and (mostly) understand it. CTCI kept mentioning Stringbuilder, but if I'm not mistaken, C++ doesn't use that so I was thrown off. Your approach seems much more simple, however I'm worried about the efficiency if asked this problem during an internship interview. What would be the BCR with this sort of solution?
@ByteByByte
@ByteByByte 7 жыл бұрын
As mentioned in the video (I think), not using a stringbuilder does affect the runtime. However, in my experience as long as you acknowledge that stringbuilders are a thing and you *could* use them, no interviewer will complain if you do it this way instead, since the code is much easier to follow
@brinderdhaliwal3570
@brinderdhaliwal3570 7 жыл бұрын
Sounds good man, greatly appreciate the input.
@itzeltravels
@itzeltravels 8 жыл бұрын
How come we don't have to use the .equals method to compare the contents of s.chartAt(i) and s.charAt(i+ 1)? I thought that using == would return false since they are at different locations in the string. Thanks in advance!
@ByteByByte
@ByteByByte 8 жыл бұрын
In this case, we're comparing a primitive type, because charAt() returns a char as opposed to string
@chaitanyakoranne
@chaitanyakoranne 5 жыл бұрын
Awesome series, but in this case, this program does not work for string - "aaabbcca" , expected output is - a4b2c2 and what we are getting is a3b2c2a1
@AmitKumarGrrowingSlow
@AmitKumarGrrowingSlow 5 жыл бұрын
char order should not be changed. If we hv to recreate the string you can't for a4b2c2...
@MrAkshay2711
@MrAkshay2711 6 жыл бұрын
Hey Sam always been fan of your videos. Learnt a lot of things. Just one doubt regarding this question, you mentioned that compressed string should be smaller than original string but case would not be possible when you have single character in string since it will always be greater than original string example "a" = "a1"; so I think return should only be compressed string and work without the check in the last? Correct me if I am wrong.
@ByteByByte
@ByteByByte 6 жыл бұрын
I'm simply saying that if the compressed string is longer, then don't compress the string. You're right that there are plenty of cases where the compressed string would be longer
@sukrithisharrma3218
@sukrithisharrma3218 7 жыл бұрын
please tell in string programs what is the meaning of int index=str[i]-'a'
@ByteByByte
@ByteByByte 7 жыл бұрын
well str[i] is a character and 'a' is a character. Both of those are represented as ASCII values, so index will be the index of the current character in the alphabet, assuming that str[i] is between 'a' and 'z'. For example 'a'-'a' = 0, 'b'-'a' = 1, 'z'-'a' = 25
@arm8733
@arm8733 7 жыл бұрын
How can we compress substrings instead of single characters in a String.For example Something like this :abababbbabba-> should output ab3bba2 ??
@ByteByByte
@ByteByByte 7 жыл бұрын
that would significantly complicate both the compressing and decompressing, but theoretically you could do it. The big question then becomes finding the optimally compressed string
@Grassmpl
@Grassmpl 7 жыл бұрын
for your 'char' issue, you could have just added the empty string first (out += ""+s.cahrAt(i)+sum)
@ByteByByte
@ByteByByte 7 жыл бұрын
certainly could although I feel like that makes the code harder to read
@Rohitsingh2410
@Rohitsingh2410 6 жыл бұрын
will this work on "aaabbbaac"
@ByteByByte
@ByteByByte 6 жыл бұрын
you tell me
@sung3898
@sung3898 7 жыл бұрын
Shouldn't the last statement be out.length() "a2b2"
@ByteByByte
@ByteByByte 7 жыл бұрын
Could go either way. I opted to return the original string unless the compression actually shortened the string
@mayankmicky5
@mayankmicky5 8 жыл бұрын
nice explanation but can u make a video on string uncompression plz :)
@ByteByByte
@ByteByByte 8 жыл бұрын
thanks! for string uncompression, you simply need to read the string character by character and whenever you see a number, duplicate the previous character that many times. I probably won't do a video about it, but I'm happy to help you with the code if you're stuck
@mayankmicky5
@mayankmicky5 8 жыл бұрын
:) okay bro
7 жыл бұрын
I had to do this problem for an interview with Google. The compression part was fine but for decompression they asked me: How do you know if the string you receive is compressed? or if it's the original string? e.g originalString = "a2b2c2"; compressedString = "a2b2c2"; result = decompress(compressedString); result should be "a2b2c2" I said you could use some sort of extra variable to check this, like the length of the original string. I guess they didn't like the answer because I didn't get the job haha. Then I thought maybe using some sort of checksum of the original string? Anyway, hope to hear what you think.
@ByteByByte
@ByteByByte 7 жыл бұрын
Interesting. The way I usually think about the problem, you just assume that strings are only a-zA-Z, but obviously if you include numbers then it gets more complicated. Even more complicated if you consider two-digit numbers because how do you know whether its all one thing or not. Off the top of my head I can think of various things you could do to encode the numbers in specific ways. For example you could use an escape character like '\' if the number should be treated as a string instead of a count. You could also come up with some other way to encode the numbers. The main problem with the solutions that you're proposing are that they tell you if your decompression is wrong, but not how to fix it :)
@zenospavlakou7991
@zenospavlakou7991 6 жыл бұрын
If you were to compress the string "abcde" using this algorithm the output would be "a1b1c1d1e1" which is twice as large. This is not compression. A good rule would be to add a number only when there are 3 or more repetitions because the string "aa" would compress to a2 which once again is not any smaller in size.
@macmath22
@macmath22 7 жыл бұрын
I think the sum should be incremented by one after while loop.
@ramachandranv.p1432
@ramachandranv.p1432 7 жыл бұрын
Wow u have been very observant with the explanation.Just striked me after u mentioned this!
@chintalapativenkataramarahul
@chintalapativenkataramarahul 6 жыл бұрын
Which loop are you talking about? I did not understand. Please could you be more specific.
@udendranmudaliyar4458
@udendranmudaliyar4458 7 жыл бұрын
kindly make more video in Java ! God bless you ! amen
@ByteByByte
@ByteByByte 7 жыл бұрын
will do. thank you :)
@jugsma6676
@jugsma6676 7 жыл бұрын
The one I did is this way, Since my git is private I copy and paste my code right here: char firstChar = givenString.charAt(0); int count = 1; String compressedString = ""; for (int i=1; i < givenString.length(); i++) { if (firstChar == givenString.charAt(i)) count++; else { compressedString += firstChar + String.valueOf(count); count = 1; firstChar = givenString.charAt(i); } if (i == givenString.length() -1) { compressedString += firstChar + String.valueOf(count); } } return compressedString;
@ByteByByte
@ByteByByte 7 жыл бұрын
Looks reasonable
@tanzeelrana7348
@tanzeelrana7348 4 жыл бұрын
public static String compress(String s) { String retString = ""; if (s.length() == 0) { return retString; } char current = s.charAt(0); int counter = 1; for (int i = 1; i < s.length(); i++) { if(s.charAt(i) != current) { retString += current; retString += counter; counter = 1; current = s.charAt(i); } else { counter++; } current = s.charAt(i); } retString += current; retString += counter; return retString; }
@tanzeelrana7348
@tanzeelrana7348 4 жыл бұрын
for your solution using the following prints s String s = "gggffd"; System.out.println(compress(s)); output is "gggffd" but it should be "g3f2d1" so I wrote the above code.
@aravindbros
@aravindbros 7 жыл бұрын
This doesnt work for the case "abb" The answer should be a1b2 but you print "abb" again since ( a1b2 - length 4 > abb - length 3).
@ByteByByte
@ByteByByte 7 жыл бұрын
that's the point. if the string doesn't actually get shorter, then you shouldn't compress it
@louiswang538
@louiswang538 5 жыл бұрын
this is not in place
@elisadevid5660
@elisadevid5660 4 жыл бұрын
Bestttt
String Compression
11:48
Kevin Naughton Jr.
Рет қаралды 96 М.
Interview Question: Find All Duplicates
18:51
Byte by Byte
Рет қаралды 41 М.
Правильный подход к детям
00:18
Beatrise
Рет қаралды 11 МЛН
Гениальное изобретение из обычного стаканчика!
00:31
Лютая физика | Олимпиадная физика
Рет қаралды 4,8 МЛН
Interview Question: Random Binary Tree
21:52
Byte by Byte
Рет қаралды 15 М.
Interview Question: Three Sum
27:13
Byte by Byte
Рет қаралды 61 М.
How Computers Compress Text: Huffman Coding and Huffman Trees
6:30
Tom Scott
Рет қаралды 1,9 МЛН
Premature Optimization
12:39
CodeAesthetic
Рет қаралды 849 М.
Naming Things in Code
7:25
CodeAesthetic
Рет қаралды 2,3 МЛН
Coding a Web Server in 25 Lines - Computerphile
17:49
Computerphile
Рет қаралды 359 М.
Interview Question: Palindromes
16:43
Byte by Byte
Рет қаралды 29 М.
Interview Question: Autocomplete
31:37
Byte by Byte
Рет қаралды 37 М.
you will never ask about pointers again after watching this video
8:03
Правильный подход к детям
00:18
Beatrise
Рет қаралды 11 МЛН