четверг, 15 ноября 2018 г.

Beautiful Binary String


Alice has a binary string. She thinks a binary string is beautiful if and only if it doesn't contain the substring "010".

In one step, Alice can change a 0 to a 1 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 b=010 she can change any one element and have a beautiful string.

Function Description

Complete the beautifulBinaryString function in the editor below. It should return an integer representing the minimum moves required.

beautifulBinaryString has the following parameter(s):

   - b: a string of binary digits

Input Format

The first line contains an integer n, the length of binary string.
The second line contains a single binary string b.

Output Format

Print the minimum number of steps needed to make the string beautiful.

Sample Input 0

Sample Output 0

Try your solution at: https://www.hackerrank.com/challenges/beautiful-binary-string/problem

#include <iostream>
#include <string>
using namespace std;

using namespace std;

// Complete the beautifulBinaryString function below.
int beautifulBinaryString(string str) {
    size_t found = str.find("010");
    int count = 0;

    while (found != string::npos)
        found = str.find("010", found + 3);
    return count;


int main()
    string b = "0100101010";

    int result = beautifulBinaryString(b);
    cout << result << "\n";
    return 0;

Комментариев нет:

Отправить комментарий

The Love-Letter Mystery

/* Джеймс раздобыл любовное письмо, которое его друг Гарри написал своей девушке. Будучи шутником, Джеймс решил изменить его. Он ...