We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Here are my C++ solution:
int beautifulBinaryString(string b) {
int i = 0, d = 0;
while (i < b.length() - 2) {
if (b[i] == '0' && b[i + 1] == '1' && b[i + 2] == '0') {
d++;
i += 3; // Skip to the character after "010"
} else {
i++;
}
}
return d;
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Beautiful Binary String
You are viewing a single comment's thread. Return to all comments →
Here are my C++ solution: int beautifulBinaryString(string b) { int i = 0, d = 0; while (i < b.length() - 2) { if (b[i] == '0' && b[i + 1] == '1' && b[i + 2] == '0') { d++; i += 3; // Skip to the character after "010" } else { i++; } } return d; }