Great explanation. This should be a hard problem. Even brute force is not quite intuitive in interview setup without hints.
@aybarskeremtaskan2 жыл бұрын
Hi, thanks for the explanation. I think the reason that extending the alternating strings (alt1 and alt2) work is that each time they transform into each other. (0101... becomes 1010... and vice versa etc.) which means that we should do diff2 = diff-1 and diff1 = diff2 -1 instead of diff -=1 and diff2-=1. The solution works because at each round we take minimum of diff1 and diff2 which makes swapping diff1 and diff2 unnecessary computation wise (even though logically it seems wrong).
@dheepthaaanand321420 күн бұрын
Thank you for this! Yes, when we are considering 110001, we would want to compare it with 010101 as well, but that comes by shifting 101010, the 2nd possible alternation. And so the diff is technically 4 becoming 2 as we should compare diff with the same alternation
@stefan.musarra6 ай бұрын
My key take-away from this problem is that shifting elements from the beginning to the end (or the opposite) is the same as a sliding window on the string doubled. Thanks for showing us that brilliant observation/trick. In this problem, if the string has an even length, you do not need to slide the window at all, but it essential to slide the window if the string has an odd length. Example: "11100" needs to be shifted 4 times but requires only 1 flip.
@madhukiranattivilli2321 Жыл бұрын
This O(n) algo can be slightly improved with these changes : Essentially, use bit manipulation operations (1) No need to have an O(n) loop to build the alternating bits mask. Bit manipualation methods and geometric progression math formula can be used to get the mask bit string in O(1) time (2) No need to have an O(n) loop to count bit flips. XOR of the input bit string segment and mask bit string segment can be done to get few 1 bits representing bit flips. Segments can be obtained by using an all 1s mask, AND with the input and alternating bits mask to get the segments, and right shift the all 1s mask to get the next segments. Later, the 1s in the XORed result can be obtained by using bit manipulation methods and geometric progression math formula -- build as many cloned blocks as is the length of the XORed result, AND each block with 1, 10, 100, ..., 1000 mask segments -- i.e. AND the 1st big block and the 2nd mask block -- right shift the ANDed block k times (where k is the length of the XORed block) to get all "bit flip indicator" bits (0 or 1) to the rightmost bit position All these can be done mathematically. No need to code. Math equation (geometric progression) gives the result in O(1) Thus the final program would be a much closer to O(n) Makes sense? (I've implemented this)
@amandubey52872 жыл бұрын
Thankyou so much neetcode, I was scared of this question until this video. Thankyou so much for explaining the logic with such details.
@DroidHolicOfficial2 жыл бұрын
Beautiful Explanation man! One thing we can do to reduce the space is to double the string only when the given string is of odd length. I noticed that for even strings, doubling is of no use. i.e., type-1 operation is of no use at all. Only for odd strings we may get a minimum flip value by doing some type-1 operations. s = s if k % 2 == 0 else s + s
@Chirayu19 Жыл бұрын
I also thought the same, Nice!
@akhilkhubchandani26323 жыл бұрын
Thanks for this , keep posting every contest . Keeps me motivated
@mohaimenchowdhury Жыл бұрын
It is great that you mentioned you have come up with that idea of type 1 operation from the leetcode discuss. It motivates people more that even NeetCodeIO looks for the leetcode discuss section, so why would we feel down to check the discuss section?
@deathbombs2 жыл бұрын
The key was seeing we can find answers by converting to a "target string", I thought there was some other trick to achieve alternating pattern, like checking for sequences
@hackytech74943 жыл бұрын
Thankyou so much..... Best explanation ever. Please keep posting for every contest 🙏
@ducthinh24122 жыл бұрын
Great explanation! Just curious, were you able to solve it during the contest, or you spent time afterwards to come up with the solution?
@maheswarreddy39173 жыл бұрын
Clean explanation man thanks. Struggling to understand from leetcode discuss section.
@alexgrossbard6206 Жыл бұрын
It is not necessary to check if left - right + 1 == n: res = min(res, diff1, diff2). You can do it at the bottom of the if left - right + 1 > n block since left will have been updated and that will make left - right + 1 == n
@udemezueiloabachie86266 ай бұрын
I always watch your explanation then I code my solution before wathcing your code solution.
@upendsingh92634 ай бұрын
to generate alt1 and alt2 we can use alt1 = "01" * n alt2 = "10" * n
@ehsanmon2 жыл бұрын
Such a smart solution. Is this really a medium question though? It's pretty hard to come up with such a solution during an interview.
@amogchandrashekar81593 жыл бұрын
Thanks for this, I was about to request this in comments!
@NeetCode3 жыл бұрын
No problem!
@neeldesai1082 жыл бұрын
Just FYI, Same implementation in Java returns TLE Thanks, ndesai
@RanjuRao2 жыл бұрын
Even I got TLE in java
@AymenDaoudi-u6h Жыл бұрын
Similar, in C#
@rajasbaadkar49896 ай бұрын
Use StringBuilder for alt1 and alt2
@arnabpersonal67293 жыл бұрын
we can get alt values by using '01'*n and '10'*n
@siddharthsinghchauhan86642 жыл бұрын
Aren't the string concatenation O(n^2) as they are immutable...
@KaKaphoebe3 жыл бұрын
very clear video. I like it. Thank you!
@Ruslanpv6bs6 ай бұрын
Super beatiful solution!
@sidazhong2019 Жыл бұрын
if (r-l+1)==n: res = min(res,diff1,diff2) This if condition is unnecessary, since the left pointer has already been increment. it's just like if (10-0+1>10), again if (10-1+1)==10 in the same loop. so completly waste. you can just put " res = min(res,diff1,diff2) " inside of the prev if condition.
@abukhandaker75586 ай бұрын
We will watch the problem or the ads? Seems like greedy algorithm
@levimatheri76825 ай бұрын
Underrated comment 😂😂
@albertlee5648 Жыл бұрын
Thanks for the video, but I think there is a mistake in the code. The string concatenation creates new copies every time, so the time complexity of constructing alt1 and alt2 is O(n^2). I think the list operation should be used instead
@ayushman_sinhaa2 жыл бұрын
Thank you so much! I Finally understood the problem
@ZQutui3 жыл бұрын
Thanks for keeping up posting new solutions. Btw, what do you do as a software engineer? Web, Mobile, backend, or something else?
@AbhishekJaiswal242 жыл бұрын
He is completely anonymous for us , he works at Google
@crit198713 жыл бұрын
Thanks for your content. Your solutions are amazing. Could you please make a video on Subarray sum equals K.
@kanehtube53908 ай бұрын
ur videos are such a pretty ones.
@hieijaganshi70887 ай бұрын
I don't understand why its impossible to get a better solution by performing type-1 operations, then type-2 operations, then type-1 operations (etc) VS just performing all the type-1 operations and then all the type-2 operations
@sairam3351 Жыл бұрын
Thank you so much bro.
@0088abs Жыл бұрын
Nice way to explain , but I think this is not the optimal solution and errors out for a test case having around 74461 characters in input . Error- :Time limit exceeded" . Kindly update the code with the upgraded version if any . It would be really helpful if there can be a direct link for the source code as well . Thanks, your videos are helpful !!!
@akashnarayan833 жыл бұрын
Thank you
@b_1729-j8j3 жыл бұрын
Thanks dude
@joeycopperson3 жыл бұрын
Does anyone know why string is added to original only once? How does duplicating string solve the problem? Is it coz string comprise of '0' and '1' (only 2 types of digits) ?
@siddharth40692 жыл бұрын
Cause its a cyclic problem. You are allowed to remove characters from the front of the string and attach to the back. Thus this will lead to the possibility of multiple different strings from a single string of length "n". If we attach the string to itself and then look at all "n" length substrings of the s, we can look at all possibilities of strings that we could have formed by removing characters from the front of the string and attaching to the end.
@victoriac72573 жыл бұрын
Hey can you please do 1889 as well? Thanks!
@yogendrakesharwani36507 ай бұрын
This is not medium level question it very very hard for anyone to come up with this type of solution by it's own
@ankithreddyadudodla76733 жыл бұрын
nice explanation!!!
@abhinavsinghkushwah24163 жыл бұрын
Thanks!
@vijaykumarlokhande16073 жыл бұрын
keep posting.
@AymenDaoudi-u6h Жыл бұрын
Efficient ?! it shows 1232ms right there, and it LTEs in other languages. But good explanation thank you.
@Sulerhy7 ай бұрын
double size of list -> too brilliant. I know the solution, but I can't figure it out how to code it effectively.
@ygwg6145 Жыл бұрын
Actually diff1 + diff2 == L. Only one diff/alt is needed.
@SmoothCode2 жыл бұрын
What is the purpose of s = s+ s ? What does it help us with?
@siddharth40692 жыл бұрын
Cause its a cyclic problem. You are allowed to remove characters from the front of the string and attach to the back. Thus this will lead to the possibility of multiple different strings from a single string of length "n". If we attach the string to itself and then look at all "n" length substrings of the s, we can look at all possibilities of strings that we could have formed by removing characters from the front of the string and attaching to the end.