Minimum number of characters to be removed to make a string alternate
Here given code implementation process.
/*
Java program for
Minimum number of characters to be removed to make a string alternate
*/
public class AlternateText
{
public void removeToAlternate(String text)
{
// Display given text
System.out.println(" Given Text " + text);
int ans = 0;
int n = text.length() - 1;
// Execute loop through by length - 1 of given text
for (int i = 0; i < n; ++i)
{
if (text.charAt(i) == text.charAt(i + 1))
{
ans++;
}
}
// Display calculated result
System.out.println(" Result : " + ans);
}
public static void main(String[] args)
{
AlternateText task = new AlternateText();
// Test Inputs
// Given Binary String
// Case A
// 100011100 = > 100011100 [1010]
// -- -- - Remove position
// Result : 5
task.removeToAlternate("100011100");
// Case B
// 1010100000101 => 101010000101 [101010101]
// --- Remove position
// Result : 3
task.removeToAlternate("101010000101");
}
}
Output
Given Text 100011100
Result : 5
Given Text 101010000101
Result : 3
// Include header file
#include <iostream>
#include <string>
using namespace std;
/*
C++ program for
Minimum number of characters to be removed to make a string alternate
*/
class AlternateText
{
public: void removeToAlternate(string text)
{
// Display given text
cout << " Given Text " << text << endl;
int ans = 0;
int n = text.length() - 1;
// Execute loop through by length - 1 of given text
for (int i = 0; i < n; ++i)
{
if (text[i] == text[i + 1])
{
ans++;
}
}
// Display calculated result
cout << " Result : " << ans << endl;
}
};
int main()
{
AlternateText *task = new AlternateText();
// Test Inputs
// Given Binary String
// Case A
// 100011100 = > 100011100 [1010]
// -- -- - Remove position
// Result : 5
task->removeToAlternate("100011100");
// Case B
// 1010100000101 => 101010000101 [101010101]
// --- Remove position
// Result : 3
task->removeToAlternate("101010000101");
return 0;
}
Output
Given Text 100011100
Result : 5
Given Text 101010000101
Result : 3
// Include namespace system
using System;
/*
Csharp program for
Minimum number of characters to be removed to make a string alternate
*/
public class AlternateText
{
public void removeToAlternate(String text)
{
// Display given text
Console.WriteLine(" Given Text " + text);
int ans = 0;
int n = text.Length - 1;
// Execute loop through by length - 1 of given text
for (int i = 0; i < n; ++i)
{
if (text[i] == text[i + 1])
{
ans++;
}
}
// Display calculated result
Console.WriteLine(" Result : " + ans);
}
public static void Main(String[] args)
{
AlternateText task = new AlternateText();
// Test Inputs
// Given Binary String
// Case A
// 100011100 = > 100011100 [1010]
// -- -- - Remove position
// Result : 5
task.removeToAlternate("100011100");
// Case B
// 1010100000101 => 101010000101 [101010101]
// --- Remove position
// Result : 3
task.removeToAlternate("101010000101");
}
}
Output
Given Text 100011100
Result : 5
Given Text 101010000101
Result : 3
package main
import "fmt"
/*
Go program for
Minimum number of characters to be removed to make a string alternate
*/
func removeToAlternate(text string) {
// Display given text
fmt.Println(" Given Text ", text)
var ans int = 0
var n int = len(text) - 1
// Execute loop through by length - 1 of given text
for i := 0 ; i < n ; i++ {
if text[i] == text[i + 1] {
ans++
}
}
// Display calculated result
fmt.Println(" Result : ", ans)
}
func main() {
// Test Inputs
// Given Binary String
// Case A
// 100011100 = > 100011100 [1010]
// -- -- - Remove position
// Result : 5
removeToAlternate("100011100")
// Case B
// 1010100000101 => 101010000101 [101010101]
// --- Remove position
// Result : 3
removeToAlternate("101010000101")
}
Output
Given Text 100011100
Result : 5
Given Text 101010000101
Result : 3
<?php
/*
Php program for
Minimum number of characters to be removed to make a string alternate
*/
class AlternateText
{
public function removeToAlternate($text)
{
// Display given text
echo(" Given Text ".$text."\n");
$ans = 0;
$n = strlen($text) - 1;
// Execute loop through by length - 1 of given text
for ($i = 0; $i < $n; ++$i)
{
if ($text[$i] == $text[$i + 1])
{
$ans++;
}
}
// Display calculated result
echo(" Result : ".$ans."\n");
}
}
function main()
{
$task = new AlternateText();
// Test Inputs
// Given Binary String
// Case A
// 100011100 = > 100011100 [1010]
// -- -- - Remove position
// Result : 5
$task->removeToAlternate("100011100");
// Case B
// 1010100000101 => 101010000101 [101010101]
// --- Remove position
// Result : 3
$task->removeToAlternate("101010000101");
}
main();
Output
Given Text 100011100
Result : 5
Given Text 101010000101
Result : 3
/*
Node JS program for
Minimum number of characters to be removed to make a string alternate
*/
class AlternateText
{
removeToAlternate(text)
{
// Display given text
console.log(" Given Text " + text);
var ans = 0;
var n = text.length - 1;
// Execute loop through by length - 1 of given text
for (var i = 0; i < n; ++i)
{
if (text.charAt(i) == text.charAt(i + 1))
{
ans++;
}
}
// Display calculated result
console.log(" Result : " + ans);
}
}
function main()
{
var task = new AlternateText();
// Test Inputs
// Given Binary String
// Case A
// 100011100 = > 100011100 [1010]
// -- -- - Remove position
// Result : 5
task.removeToAlternate("100011100");
// Case B
// 1010100000101 => 101010000101 [101010101]
// --- Remove position
// Result : 3
task.removeToAlternate("101010000101");
}
main();
Output
Given Text 100011100
Result : 5
Given Text 101010000101
Result : 3
# Python 3 program for
# Minimum number of characters to be removed to make a string alternate
class AlternateText :
def removeToAlternate(self, text) :
# Display given text
print(" Given Text ", text)
ans = 0
n = len(text) - 1
i = 0
# Execute loop through by length - 1 of given text
while (i < n) :
if (text[i] == text[i + 1]) :
ans += 1
i += 1
# Display calculated result
print(" Result : ", ans)
def main() :
task = AlternateText()
# Test Inputs
# Given Binary String
# Case A
# 100011100 = > 100011100 [1010]
# -- -- - Remove position
# Result : 5
task.removeToAlternate("100011100")
# Case B
# 1010100000101 => 101010000101 [101010101]
# --- Remove position
# Result : 3
task.removeToAlternate("101010000101")
if __name__ == "__main__": main()
Output
Given Text 100011100
Result : 5
Given Text 101010000101
Result : 3
# Ruby program for
# Minimum number of characters to be removed to make a string alternate
class AlternateText
def removeToAlternate(text)
# Display given text
print(" Given Text ", text, "\n")
ans = 0
n = text.length - 1
i = 0
# Execute loop through by length - 1 of given text
while (i < n)
if (text[i] == text[i + 1])
ans += 1
end
i += 1
end
# Display calculated result
print(" Result : ", ans, "\n")
end
end
def main()
task = AlternateText.new()
# Test Inputs
# Given Binary String
# Case A
# 100011100 = > 100011100 [1010]
# -- -- - Remove position
# Result : 5
task.removeToAlternate("100011100")
# Case B
# 1010100000101 => 101010000101 [101010101]
# --- Remove position
# Result : 3
task.removeToAlternate("101010000101")
end
main()
Output
Given Text 100011100
Result : 5
Given Text 101010000101
Result : 3
import scala.collection.mutable._;
/*
Scala program for
Minimum number of characters to be removed to make a string alternate
*/
class AlternateText()
{
def removeToAlternate(text: String): Unit = {
// Display given text
println(" Given Text " + text);
var ans: Int = 0;
var n: Int = text.length() - 1;
var i: Int = 0;
// Execute loop through by length - 1 of given text
while (i < n)
{
if (text.charAt(i) == text.charAt(i + 1))
{
ans += 1;
}
i += 1;
}
// Display calculated result
println(" Result : " + ans);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: AlternateText = new AlternateText();
// Test Inputs
// Given Binary String
// Case A
// 100011100 = > 100011100 [1010]
// -- -- - Remove position
// Result : 5
task.removeToAlternate("100011100");
// Case B
// 1010100000101 => 101010000101 [101010101]
// --- Remove position
// Result : 3
task.removeToAlternate("101010000101");
}
}
Output
Given Text 100011100
Result : 5
Given Text 101010000101
Result : 3
import Foundation;
/*
Swift 4 program for
Minimum number of characters to be removed to make a string alternate
*/
class AlternateText
{
func removeToAlternate(_ data: String)
{
// Display given text
print(" Given Text ", data);
var ans: Int = 0;
let n: Int = data.count - 1;
var i: Int = 0;
let text = Array(data);
// Execute loop through by length - 1 of given text
while (i < n)
{
if (text[i] == text[i + 1])
{
ans += 1;
}
i += 1;
}
// Display calculated result
print(" Result : ", ans);
}
}
func main()
{
let task: AlternateText = AlternateText();
// Test Inputs
// Given Binary String
// Case A
// 100011100 = > 100011100 [1010]
// -- -- - Remove position
// Result : 5
task.removeToAlternate("100011100");
// Case B
// 1010100000101 => 101010000101 [101010101]
// --- Remove position
// Result : 3
task.removeToAlternate("101010000101");
}
main();
Output
Given Text 100011100
Result : 5
Given Text 101010000101
Result : 3
/*
Kotlin program for
Minimum number of characters to be removed to make a string alternate
*/
class AlternateText
{
fun removeToAlternate(text: String): Unit
{
// Display given text
println(" Given Text " + text);
var ans: Int = 0;
val n: Int = text.length - 1;
var i: Int = 0;
// Execute loop through by length - 1 of given text
while (i < n)
{
if (text.get(i) == text.get(i + 1))
{
ans += 1;
}
i += 1;
}
// Display calculated result
println(" Result : " + ans);
}
}
fun main(args: Array < String > ): Unit
{
val task: AlternateText = AlternateText();
// Test Inputs
// Given Binary String
// Case A
// 100011100 = > 100011100 [1010]
// -- -- - Remove position
// Result : 5
task.removeToAlternate("100011100");
// Case B
// 1010100000101 => 101010000101 [101010101]
// --- Remove position
// Result : 3
task.removeToAlternate("101010000101");
}
Output
Given Text 100011100
Result : 5
Given Text 101010000101
Result : 3
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