String Question | JPMorgan

String question | JPMorgan

Can you post the question link??

I was asked this in the interview, is anything unclear?

I didn’t get the string conversion.

You need to make the minimum number of changes in string S such that it has x number of 1′s in the beginning, followed by the remaining (length−x) number of 0′s.

You can change any 1 to 0 and vice versa

for every index, store the number of 0’s to its left and number of 1’s to its right. we just have to minimize the sum.

It’s literally just this

for c in s:
        ans=min(ans, curr)

x=0 case, we convert all to 0s. then we go forward. If it’s a 0, then for this x, we’ll need one more change, if it’s a 1, we’ll need one less change, and take minimum.


Can you pls elaborate

if x=0, then we convert every ‘1’ to a ‘0’. So base answer = s.count(‘1’)
Then if we increase x, we have to look at the next value. If it’s a ‘0’, We’ll have to convert this also to a ‘1’, so the current answer will increase. If it’s a ‘1’, Then we won’t have to convert it to a ‘0’, so the current answer will decrease. Just take minimum of that.

Dude like literally it’s exactly the same question from hackerearth circuits 2 problem.
There it was a string of A’s and B’s and here u changed it as 1’s and 0’s :unamused: :unamused: