Beautiful Binary String

Sort by

recency

|

854 Discussions

|

  • + 0 comments

    // c#

    public static int beautifulBinaryString(string b)
    {       
        string newB = b.Replace("010", "xxx");
        int occur = 0;
    
        for(int i=0; i<newB.Length; i++)
        {
            if (newB.Substring(i,1) == "x")
            {
                i += 2;
                occur++;
            }
        }
    
        return occur;
    }
    
  • + 0 comments

    Basic solution

    int beautifulBinaryString(string b) { int result = 0; while (b.find("010")!=string::npos) { ++b[b.find("010")+2]; result++; } return result; }

  • + 0 comments
    def beautifulBinaryString(b):
        # Write your code here
        operations = 0
        count = 0
        while count < len(b) - 2:
            if b[count: count+3] == "010":
                operations += 1
                count += 3
                continue
            count+= 1
        return operations
    
  • + 0 comments

    Here is problem solution in python java c++ c and javascript - https://programmingoneonone.com/hackerrank-beautiful-binary-string-problem-solution.html

  • + 0 comments

    python 3

    def beautifulBinaryString(b):
        l = list(b)
        steps = 0
        
        for i in range(len(b)-2):
            if "".join(l[i : i + 3]) == '010':
                steps += 1
                if i + 3 < len(b) and l[i + 3] == '1':
                    l[i + 2] = '1'
                else:
                    l[i + 1] = '0'
        
        return steps