Untitled

 avatar
unknown
plain_text
3 months ago
716 B
4
Indexable
class Solution {
    public long minimumSteps(String s) {
       // 1 - black
       // 0 - white
    //    00000000000000001111111111111111
      //  mini
        long nextIndexPlacement = 0; // Tracks where the next '1' should be placed
        long swaps = 0; // Count of swaps required
        int i = 0;
        while(i<s.length() && s.charAt(i)!='1'){
            i++;
        }
        if(i<s.length()){
           nextIndexPlacement = i;
        }
        while(i<s.length())
        {
            if(s.charAt(i) == '0'){
               swaps += i - nextIndexPlacement;
               nextIndexPlacement++;
            }
            i++; 
        }   
        return swaps;

    }
}
Editor is loading...
Leave a Comment