Alice has a binary string. She thinks a binary string is beautiful if and only if it doesn't contain the substring .
In one step, Alice can change a  to a  or vice versa. Count and print the minimum number of steps needed to make Alice see the string as beautiful.
For example, if Alice's string is  she can change any one element and have a beautiful string.

Question from hackerrank.

Solution in C# - 


class Solution {
    static int beautifulBinaryString(string b) {
        string a = b.Replace("010","");
        int len = b.Length - a.Length;
        return len/3;
    }
    static void Main(string[] args) {
        TextWriter textWriter = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);
        int n = Convert.ToInt32(Console.ReadLine());
        string b = Console.ReadLine();
        int result = beautifulBinaryString(b);
        textWriter.WriteLine(result);
        textWriter.Flush();
        textWriter.Close();
    }
}


Post a Comment

Previous Post Next Post