Number of flips to make binary string alternate
Given a binary string (a string consisting of only '0's and '1's), we can flip some of the bits to make the string alternate. An alternating string is a string where no two adjacent characters are the same. For example, "101010" is an alternating string.
To find the minimum number of flips needed to make a binary string alternate, we can count the number of flips needed for two cases: starting with '0' and starting with '1'. We choose the case that requires the fewest flips.
Here given code implementation process.
/*
Java program for
Number of flips to make binary string alternate
*/
public class AlternateText
{
public int countChanges(String text, char nextChar)
{
int ans = 0;
// Execute loop through by length of given text
for (int i = 0; i < text.length(); ++i)
{
if (text.charAt(i) != nextChar)
{
ans++;
}
// Change next character
if (nextChar == '1')
{
nextChar = '0';
}
else
{
nextChar = '1';
}
}
return ans;
}
void flipToAlternate(String text)
{
// Count number of alternate sequence change of 01.. in text
int alternateByZero = countChanges(text, '0');
// Count number of alternate sequence change of 10.. in text
int alternateByOne = countChanges(text, '1');
System.out.println(" Given Text " + text);
if (alternateByOne > alternateByZero)
{
// Sequence of 01010101...
System.out.println(" Result : " + alternateByZero);
}
else
{
// Sequence of 10101010...
System.out.println(" Result : " + alternateByOne);
}
}
public static void main(String[] args)
{
AlternateText task = new AlternateText();
// Test Inputs
// Given Binary String
// Case A
// 001100010000 ==> 010101010101
// -- - - - -- - - -
// Result : 5
task.flipToAlternate("001100010000");
// Case B
// 1010100000101 => 1010101010101
// - - - -
// Result : 2
task.flipToAlternate("1010100000101");
}
}
Output
Given Text 001100010000
Result : 5
Given Text 1010100000101
Result : 2
// Include header file
#include <iostream>
#include <string>
using namespace std;
/*
C++ program for
Number of flips to make binary string alternate
*/
class AlternateText
{
public: int countChanges(string text, char nextChar)
{
int ans = 0;
// Execute loop through by length of given text
for (int i = 0; i < text.length(); ++i)
{
if (text[i] != nextChar)
{
ans++;
}
// Change next character
if (nextChar == '1')
{
nextChar = '0';
}
else
{
nextChar = '1';
}
}
return ans;
}
void flipToAlternate(string text)
{
// Count number of alternate sequence change of 01.. in text
int alternateByZero = this->countChanges(text, '0');
// Count number of alternate sequence change of 10.. in text
int alternateByOne = this->countChanges(text, '1');
cout << " Given Text " << text << endl;
if (alternateByOne > alternateByZero)
{
// Sequence of 01010101...
cout << " Result : " << alternateByZero << endl;
}
else
{
// Sequence of 10101010...
cout << " Result : " << alternateByOne << endl;
}
}
};
int main()
{
AlternateText *task = new AlternateText();
// Test Inputs
// Given Binary String
// Case A
// 001100010000 ==> 010101010101
// -- - - - -- - - -
// Result : 5
task->flipToAlternate("001100010000");
// Case B
// 1010100000101 => 1010101010101
// - - - -
// Result : 2
task->flipToAlternate("1010100000101");
return 0;
}
Output
Given Text 001100010000
Result : 5
Given Text 1010100000101
Result : 2
// Include namespace system
using System;
/*
Csharp program for
Number of flips to make binary string alternate
*/
public class AlternateText
{
public int countChanges(String text, char nextChar)
{
int ans = 0;
// Execute loop through by length of given text
for (int i = 0; i < text.Length; ++i)
{
if (text[i] != nextChar)
{
ans++;
}
// Change next character
if (nextChar == '1')
{
nextChar = '0';
}
else
{
nextChar = '1';
}
}
return ans;
}
void flipToAlternate(String text)
{
// Count number of alternate sequence change of 01.. in text
int alternateByZero = this.countChanges(text, '0');
// Count number of alternate sequence change of 10.. in text
int alternateByOne = this.countChanges(text, '1');
Console.WriteLine(" Given Text " + text);
if (alternateByOne > alternateByZero)
{
// Sequence of 01010101...
Console.WriteLine(" Result : " + alternateByZero);
}
else
{
// Sequence of 10101010...
Console.WriteLine(" Result : " + alternateByOne);
}
}
public static void Main(String[] args)
{
AlternateText task = new AlternateText();
// Test Inputs
// Given Binary String
// Case A
// 001100010000 ==> 010101010101
// -- - - - -- - - -
// Result : 5
task.flipToAlternate("001100010000");
// Case B
// 1010100000101 => 1010101010101
// - - - -
// Result : 2
task.flipToAlternate("1010100000101");
}
}
Output
Given Text 001100010000
Result : 5
Given Text 1010100000101
Result : 2
# Ruby program for
# Number of flips to make binary string alternate
class AlternateText
def countChanges(text, nextChar)
ans = 0
i = 0
# Execute loop through by length of given text
while (i < text.length)
if (text[i] != nextChar)
ans += 1
end
# Change next character
if (nextChar == '1')
nextChar = '0'
else
nextChar = '1'
end
i += 1
end
return ans
end
def flipToAlternate(text)
# Count number of alternate sequence change of 01.. in text
alternateByZero = self.countChanges(text, '0')
# Count number of alternate sequence change of 10.. in text
alternateByOne = self.countChanges(text, '1')
print(" Given Text ", text, "\n")
if (alternateByOne > alternateByZero)
# Sequence of 01010101...
print(" Result : ", alternateByZero, "\n")
else
# Sequence of 10101010...
print(" Result : ", alternateByOne, "\n")
end
end
end
def main()
task = AlternateText.new()
# Test Inputs
# Given Binary String
# Case A
# 001100010000 ==> 010101010101
# -- - - - -- - - -
# Result : 5
task.flipToAlternate("001100010000")
# Case B
# 1010100000101 => 1010101010101
# - - - -
# Result : 2
task.flipToAlternate("1010100000101")
end
main()
Output
Given Text 001100010000
Result : 5
Given Text 1010100000101
Result : 2
<?php
/*
Php program for
Number of flips to make binary string alternate
*/
class AlternateText
{
public function countChanges($text, $nextChar)
{
$ans = 0;
// Execute loop through by length of given text
for ($i = 0; $i < strlen($text); ++$i)
{
if ($text[$i] != $nextChar)
{
$ans++;
}
// Change next character
if ($nextChar == '1')
{
$nextChar = '0';
}
else
{
$nextChar = '1';
}
}
return $ans;
}
function flipToAlternate($text)
{
// Count number of alternate sequence change of 01.. in text
$alternateByZero = $this->countChanges($text, '0');
// Count number of alternate sequence change of 10.. in text
$alternateByOne = $this->countChanges($text, '1');
echo(" Given Text ".$text."\n");
if ($alternateByOne > $alternateByZero)
{
// Sequence of 01010101...
echo(" Result : ".$alternateByZero."\n");
}
else
{
// Sequence of 10101010...
echo(" Result : ".$alternateByOne."\n");
}
}
}
function main()
{
$task = new AlternateText();
// Test Inputs
// Given Binary String
// Case A
// 001100010000 ==> 010101010101
// -- - - - -- - - -
// Result : 5
$task->flipToAlternate("001100010000");
// Case B
// 1010100000101 => 1010101010101
// - - - -
// Result : 2
$task->flipToAlternate("1010100000101");
}
main();
Output
Given Text 001100010000
Result : 5
Given Text 1010100000101
Result : 2
/*
Node JS program for
Number of flips to make binary string alternate
*/
class AlternateText
{
countChanges(text, nextChar)
{
var ans = 0;
// Execute loop through by length of given text
for (var i = 0; i < text.length; ++i)
{
if (text.charAt(i) != nextChar)
{
ans++;
}
// Change next character
if (nextChar == '1')
{
nextChar = '0';
}
else
{
nextChar = '1';
}
}
return ans;
}
flipToAlternate(text)
{
// Count number of alternate sequence change of 01.. in text
var alternateByZero = this.countChanges(text, '0');
// Count number of alternate sequence change of 10.. in text
var alternateByOne = this.countChanges(text, '1');
console.log(" Given Text " + text);
if (alternateByOne > alternateByZero)
{
// Sequence of 01010101...
console.log(" Result : " + alternateByZero);
}
else
{
// Sequence of 10101010...
console.log(" Result : " + alternateByOne);
}
}
}
function main()
{
var task = new AlternateText();
// Test Inputs
// Given Binary String
// Case A
// 001100010000 ==> 010101010101
// -- - - - -- - - -
// Result : 5
task.flipToAlternate("001100010000");
// Case B
// 1010100000101 => 1010101010101
// - - - -
// Result : 2
task.flipToAlternate("1010100000101");
}
main();
Output
Given Text 001100010000
Result : 5
Given Text 1010100000101
Result : 2
# Python 3 program for
# Number of flips to make binary string alternate
class AlternateText :
def countChanges(self, text, nextChar) :
ans = 0
i = 0
# Execute loop through by length of given text
while (i < len(text)) :
if (text[i] != nextChar) :
ans += 1
# Change next character
if (nextChar == '1') :
nextChar = '0'
else :
nextChar = '1'
i += 1
return ans
def flipToAlternate(self, text) :
# Count number of alternate sequence change of 01.. in text
alternateByZero = self.countChanges(text, '0')
# Count number of alternate sequence change of 10.. in text
alternateByOne = self.countChanges(text, '1')
print(" Given Text ", text)
if (alternateByOne > alternateByZero) :
# Sequence of 01010101...
print(" Result : ", alternateByZero)
else :
# Sequence of 10101010...
print(" Result : ", alternateByOne)
def main() :
task = AlternateText()
# Test Inputs
# Given Binary String
# Case A
# 001100010000 ==> 010101010101
# -- - - - -- - - -
# Result : 5
task.flipToAlternate("001100010000")
# Case B
# 1010100000101 => 1010101010101
# - - - -
# Result : 2
task.flipToAlternate("1010100000101")
if __name__ == "__main__": main()
Output
Given Text 001100010000
Result : 5
Given Text 1010100000101
Result : 2
package main
import "fmt"
/*
Go program for
Number of flips to make binary string alternate
*/
func countChanges(text string, nextChar byte) int {
var ans int = 0
// Execute loop through by length of given text
for i := 0 ; i < len(text) ; i++ {
if text[i] != nextChar {
ans++
}
// Change next character
if nextChar == '1' {
nextChar = '0'
} else {
nextChar = '1'
}
}
return ans
}
func flipToAlternate(text string) {
// Count number of alternate sequence change of 01.. in text
var alternateByZero int = countChanges(text, '0')
// Count number of alternate sequence change of 10.. in text
var alternateByOne int = countChanges(text, '1')
fmt.Println(" Given Text ", text)
if alternateByOne > alternateByZero {
// Sequence of 01010101...
fmt.Println(" Result : ", alternateByZero)
} else {
// Sequence of 10101010...
fmt.Println(" Result : ", alternateByOne)
}
}
func main() {
// Test Inputs
// Given Binary String
// Case A
// 001100010000 ==> 010101010101
// -- - - - -- - - -
// Result : 5
flipToAlternate("001100010000")
// Case B
// 1010100000101 => 1010101010101
// - - - -
// Result : 2
flipToAlternate("1010100000101")
}
Output
Given Text 001100010000
Result : 5
Given Text 1010100000101
Result : 2
import scala.collection.mutable._;
/*
Scala program for
Number of flips to make binary string alternate
*/
class AlternateText()
{
def countChanges(text: String, next: Char): Int = {
var ans: Int = 0;
var i: Int = 0;
var nextChar = next;
// Execute loop through by length of given text
while (i < text.length())
{
if (text.charAt(i) != nextChar)
{
ans += 1;
}
// Change next character
if (nextChar == '1')
{
nextChar = '0';
}
else
{
nextChar = '1';
}
i += 1;
}
return ans;
}
def flipToAlternate(text: String): Unit = {
// Count number of alternate sequence change of 01.. in text
var alternateByZero: Int = countChanges(text, '0');
// Count number of alternate sequence change of 10.. in text
var alternateByOne: Int = countChanges(text, '1');
println(" Given Text " + text);
if (alternateByOne > alternateByZero)
{
// Sequence of 01010101...
println(" Result : " + alternateByZero);
}
else
{
// Sequence of 10101010...
println(" Result : " + alternateByOne);
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: AlternateText = new AlternateText();
// Test Inputs
// Given Binary String
// Case A
// 001100010000 ==> 010101010101
// -- - - - -- - - -
// Result : 5
task.flipToAlternate("001100010000");
// Case B
// 1010100000101 => 1010101010101
// - - - -
// Result : 2
task.flipToAlternate("1010100000101");
}
}
Output
Given Text 001100010000
Result : 5
Given Text 1010100000101
Result : 2
import Foundation;
/*
Swift 4 program for
Number of flips to make binary string alternate
*/
class AlternateText
{
func countChanges(_ text: [Character], _ next: Character) -> Int
{
var ans: Int = 0;
var i: Int = 0;
var nextChar = next;
// Execute loop through by length of given text
while (i < text.count)
{
if (text[i] != nextChar)
{
ans += 1;
}
// Change next character
if (nextChar == "1")
{
nextChar = "0";
}
else
{
nextChar = "1";
}
i += 1;
}
return ans;
}
func flipToAlternate(_ text: String)
{
// Count number of alternate sequence change of 01.. in text
let alternateByZero: Int = self.countChanges(Array(text), "0");
// Count number of alternate sequence change of 10.. in text
let alternateByOne: Int = self.countChanges(Array(text), "1");
print(" Given Text ", text);
if (alternateByOne > alternateByZero)
{
// Sequence of 01010101...
print(" Result : ", alternateByZero);
}
else
{
// Sequence of 10101010...
print(" Result : ", alternateByOne);
}
}
}
func main()
{
let task: AlternateText = AlternateText();
// Test Inputs
// Given Binary String
// Case A
// 001100010000 ==> 010101010101
// -- - - - -- - - -
// Result : 5
task.flipToAlternate("001100010000");
// Case B
// 1010100000101 => 1010101010101
// - - - -
// Result : 2
task.flipToAlternate("1010100000101");
}
main();
Output
Given Text 001100010000
Result : 5
Given Text 1010100000101
Result : 2
/*
Kotlin program for
Number of flips to make binary string alternate
*/
class AlternateText
{
fun countChanges(text: String, next: Char): Int
{
var ans: Int = 0;
var i: Int = 0;
var nextChar : Char = next;
// Execute loop through by length of given text
while (i < text.length)
{
if (text.get(i) != nextChar)
{
ans += 1;
}
// Change next character
if (nextChar == '1')
{
nextChar = '0';
}
else
{
nextChar = '1';
}
i += 1;
}
return ans;
}
fun flipToAlternate(text: String): Unit
{
// Count number of alternate sequence change of 01.. in text
val alternateByZero: Int = this.countChanges(text, '0');
// Count number of alternate sequence change of 10.. in text
val alternateByOne: Int = this.countChanges(text, '1');
println(" Given Text " + text);
if (alternateByOne > alternateByZero)
{
// Sequence of 01010101...
println(" Result : " + alternateByZero);
}
else
{
// Sequence of 10101010...
println(" Result : " + alternateByOne);
}
}
}
fun main(args: Array < String > ): Unit
{
val task: AlternateText = AlternateText();
// Test Inputs
// Given Binary String
// Case A
// 001100010000 ==> 010101010101
// -- - - - -- - - -
// Result : 5
task.flipToAlternate("001100010000");
// Case B
// 1010100000101 => 1010101010101
// - - - -
// Result : 2
task.flipToAlternate("1010100000101");
}
Output
Given Text 001100010000
Result : 5
Given Text 1010100000101
Result : 2
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment