Reduce the string by removing k consecutive identical characters
Here given code implementation process.
/*
Java Program for
Reduce the string by removing k consecutive identical characters
*/
public class RemoveCharacters
{
public void removeConsecutiveKidentical(String text, int k)
{
// Get the length
int n = text.length();
if (n == 0)
{
return;
}
String result = "";
int i = 0;
int j = 0;
while (i < n)
{
j = 0;
while (j < k && j + i < n && text.charAt(j + i) == text.charAt(i))
{
j++;
}
if (j == k)
{
// skip k consecutive characters
i += j;
}
else
{
result = result + text.charAt(i);
i++;
}
}
// Display result
System.out.println(" Given text : " + text);
System.out.println(" Given K : " + k);
System.out.println(" Result : " + result);
}
public static void main(String[] args)
{
RemoveCharacters task = new RemoveCharacters();
// Test
// N = 3
task.removeConsecutiveKidentical("iiiiiceooookddeeee",3);
// N = 2
task.removeConsecutiveKidentical("caaooooodeees",2);
}
}
Output
Given text : iiiiiceooookddeeee
Given K : 3
Result : iiceokdde
Given text : caaooooodeees
Given K : 2
Result : codes
// Include header file
#include <iostream>
#include <string>
using namespace std;
/*
C++ Program for
Reduce the string by removing k consecutive identical characters
*/
class RemoveCharacters
{
public: void removeConsecutiveKidentical(string text, int k)
{
// Get the length
int n = text.length();
if (n == 0)
{
return;
}
string result = "";
int i = 0;
int j = 0;
while (i < n)
{
j = 0;
while (j < k && j + i < n && text[j + i] == text[i])
{
j++;
}
if (j == k)
{
// Skip k consecutive characters
i += j;
}
else
{
result = result + (text[i]);
i++;
}
}
// Display result
cout << " Given text : " << text << endl;
cout << " Given K : " << k << endl;
cout << " Result : " << result << endl;
}
};
int main()
{
RemoveCharacters *task = new RemoveCharacters();
// Test
// N = 3
task->removeConsecutiveKidentical("iiiiiceooookddeeee", 3);
// N = 2
task->removeConsecutiveKidentical("caaooooodeees", 2);
return 0;
}
Output
Given text : iiiiiceooookddeeee
Given K : 3
Result : iiceokdde
Given text : caaooooodeees
Given K : 2
Result : codes
package main
import "fmt"
/*
Go Program for
Reduce the string by removing k consecutive identical characters
*/
type RemoveCharacters struct {}
func getRemoveCharacters() * RemoveCharacters {
var me *RemoveCharacters = &RemoveCharacters {}
return me
}
func(this RemoveCharacters) removeConsecutiveKidentical(text string, k int) {
// Get the length
var n int = len(text)
if n == 0 {
return
}
var result string = ""
var i int = 0
var j int = 0
for (i < n) {
j = 0
for (j < k && j + i < n && text[j + i] == text[i]) {
j++
}
if j == k {
// Skip k consecutive characters
i += j
} else {
result = result + string(text[i])
i++
}
}
// Display result
fmt.Println(" Given text : ", text)
fmt.Println(" Given K : ", k)
fmt.Println(" Result : ", result)
}
func main() {
var task * RemoveCharacters = getRemoveCharacters()
// Test
// N = 3
task.removeConsecutiveKidentical("iiiiiceooookddeeee", 3)
// N = 2
task.removeConsecutiveKidentical("caaooooodeees", 2)
}
Output
Given text : iiiiiceooookddeeee
Given K : 3
Result : iiceokdde
Given text : caaooooodeees
Given K : 2
Result : codes
// Include namespace system
using System;
/*
Csharp Program for
Reduce the string by removing k consecutive identical characters
*/
public class RemoveCharacters
{
public void removeConsecutiveKidentical(String text, int k)
{
// Get the length
int n = text.Length;
if (n == 0)
{
return;
}
String result = "";
int i = 0;
int j = 0;
while (i < n)
{
j = 0;
while (j < k && j + i < n && text[j + i] == text[i])
{
j++;
}
if (j == k)
{
// Skip k consecutive characters
i += j;
}
else
{
result = result + text[i];
i++;
}
}
// Display result
Console.WriteLine(" Given text : " + text);
Console.WriteLine(" Given K : " + k);
Console.WriteLine(" Result : " + result);
}
public static void Main(String[] args)
{
RemoveCharacters task = new RemoveCharacters();
// Test
// N = 3
task.removeConsecutiveKidentical("iiiiiceooookddeeee", 3);
// N = 2
task.removeConsecutiveKidentical("caaooooodeees", 2);
}
}
Output
Given text : iiiiiceooookddeeee
Given K : 3
Result : iiceokdde
Given text : caaooooodeees
Given K : 2
Result : codes
<?php
/*
Php Program for
Reduce the string by removing k consecutive identical characters
*/
class RemoveCharacters
{
public function removeConsecutiveKidentical($text, $k)
{
// Get the length
$n = strlen($text);
if ($n == 0)
{
return;
}
$result = "";
$i = 0;
$j = 0;
while ($i < $n)
{
$j = 0;
while ($j < $k && $j + $i < $n && $text[$j + $i] == $text[$i])
{
$j++;
}
if ($j == $k)
{
// Skip k consecutive characters
$i += $j;
}
else
{
$result = $result.strval($text[$i]);
$i++;
}
}
// Display result
echo(" Given text : ".$text.
"\n");
echo(" Given K : ".$k.
"\n");
echo(" Result : ".$result.
"\n");
}
}
function main()
{
$task = new RemoveCharacters();
// Test
// N = 3
$task->removeConsecutiveKidentical("iiiiiceooookddeeee", 3);
// N = 2
$task->removeConsecutiveKidentical("caaooooodeees", 2);
}
main();
Output
Given text : iiiiiceooookddeeee
Given K : 3
Result : iiceokdde
Given text : caaooooodeees
Given K : 2
Result : codes
/*
Node JS Program for
Reduce the string by removing k consecutive identical characters
*/
class RemoveCharacters
{
removeConsecutiveKidentical(text, k)
{
// Get the length
var n = text.length;
if (n == 0)
{
return;
}
var result = "";
var i = 0;
var j = 0;
while (i < n)
{
j = 0;
while (j < k && j + i < n &&
text.charAt(j + i) == text.charAt(i))
{
j++;
}
if (j == k)
{
// Skip k consecutive characters
i += j;
}
else
{
result = result + text.charAt(i);
i++;
}
}
// Display result
console.log(" Given text : " + text);
console.log(" Given K : " + k);
console.log(" Result : " + result);
}
}
function main()
{
var task = new RemoveCharacters();
// Test
// N = 3
task.removeConsecutiveKidentical("iiiiiceooookddeeee", 3);
// N = 2
task.removeConsecutiveKidentical("caaooooodeees", 2);
}
main();
Output
Given text : iiiiiceooookddeeee
Given K : 3
Result : iiceokdde
Given text : caaooooodeees
Given K : 2
Result : codes
# Python 3 Program for
# Reduce the string by removing k consecutive identical characters
class RemoveCharacters :
def removeConsecutiveKidentical(self, text, k) :
# Get the length
n = len(text)
if (n == 0) :
return
result = ""
i = 0
j = 0
while (i < n) :
j = 0
while (j < k and j + i < n and text[j + i] == text[i]) :
j += 1
if (j == k) :
# Skip k consecutive characters
i += j
else :
result = result + str(text[i])
i += 1
# Display result
print(" Given text : ", text)
print(" Given K : ", k)
print(" Result : ", result)
def main() :
task = RemoveCharacters()
# Test
# N = 3
task.removeConsecutiveKidentical("iiiiiceooookddeeee", 3)
# N = 2
task.removeConsecutiveKidentical("caaooooodeees", 2)
if __name__ == "__main__": main()
Output
Given text : iiiiiceooookddeeee
Given K : 3
Result : iiceokdde
Given text : caaooooodeees
Given K : 2
Result : codes
# Ruby Program for
# Reduce the string by removing k consecutive identical characters
class RemoveCharacters
def removeConsecutiveKidentical(text, k)
# Get the length
n = text.length
if (n == 0)
return
end
result = ""
i = 0
j = 0
while (i < n)
j = 0
while (j < k && j + i < n && text[j + i] == text[i])
j += 1
end
if (j == k)
# Skip k consecutive characters
i += j
else
result = result + text[i].to_s
i += 1
end
end
# Display result
print(" Given text : ", text, "\n")
print(" Given K : ", k, "\n")
print(" Result : ", result, "\n")
end
end
def main()
task = RemoveCharacters.new()
# Test
# N = 3
task.removeConsecutiveKidentical("iiiiiceooookddeeee", 3)
# N = 2
task.removeConsecutiveKidentical("caaooooodeees", 2)
end
main()
Output
Given text : iiiiiceooookddeeee
Given K : 3
Result : iiceokdde
Given text : caaooooodeees
Given K : 2
Result : codes
import scala.collection.mutable._;
/*
Scala Program for
Reduce the string by removing k consecutive identical characters
*/
class RemoveCharacters()
{
def removeConsecutiveKidentical(text: String, k: Int): Unit = {
// Get the length
var n: Int = text.length();
if (n == 0)
{
return;
}
var result: String = "";
var i: Int = 0;
var j: Int = 0;
while (i < n)
{
j = 0;
while (j < k && j + i < n &&
text.charAt(j + i) == text.charAt(i))
{
j += 1;
}
if (j == k)
{
// Skip k consecutive characters
i += j;
}
else
{
result = result + text.charAt(i).toString();
i += 1;
}
}
// Display result
println(" Given text : " + text);
println(" Given K : " + k);
println(" Result : " + result);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: RemoveCharacters = new RemoveCharacters();
// Test
// N = 3
task.removeConsecutiveKidentical("iiiiiceooookddeeee", 3);
// N = 2
task.removeConsecutiveKidentical("caaooooodeees", 2);
}
}
Output
Given text : iiiiiceooookddeeee
Given K : 3
Result : iiceokdde
Given text : caaooooodeees
Given K : 2
Result : codes
import Foundation;
/*
Swift 4 Program for
Reduce the string by removing k consecutive identical characters
*/
class RemoveCharacters
{
func removeConsecutiveKidentical(_ data: String, _ k: Int)
{
let text = Array(data);
// Get the length
let n: Int = text.count;
if (n == 0)
{
return;
}
var result: String = "";
var i: Int = 0;
var j: Int = 0;
while (i < n)
{
j = 0;
while (j < k && j + i < n && text[j + i] == text[i])
{
j += 1;
}
if (j == k)
{
// Skip k consecutive characters
i += j;
}
else
{
result = result + String(text[i]);
i += 1;
}
}
// Display result
print(" Given text : ", data);
print(" Given K : ", k);
print(" Result : ", result);
}
}
func main()
{
let task: RemoveCharacters = RemoveCharacters();
// Test
// N = 3
task.removeConsecutiveKidentical("iiiiiceooookddeeee", 3);
// N = 2
task.removeConsecutiveKidentical("caaooooodeees", 2);
}
main();
Output
Given text : iiiiiceooookddeeee
Given K : 3
Result : iiceokdde
Given text : caaooooodeees
Given K : 2
Result : codes
/*
Kotlin Program for
Reduce the string by removing k consecutive identical characters
*/
class RemoveCharacters
{
fun removeConsecutiveKidentical(text: String, k: Int): Unit
{
// Get the length
val n: Int = text.length;
if (n == 0)
{
return;
}
var result: String = "";
var i: Int = 0;
var j: Int ;
while (i < n)
{
j = 0;
while (j < k && j + i < n && text.get(j + i) == text.get(i))
{
j += 1;
}
if (j == k)
{
// Skip k consecutive characters
i += j;
}
else
{
result = result + text.get(i).toString();
i += 1;
}
}
// Display result
println(" Given text : " + text);
println(" Given K : " + k);
println(" Result : " + result);
}
}
fun main(args: Array < String > ): Unit
{
val task: RemoveCharacters = RemoveCharacters();
// Test
// N = 3
task.removeConsecutiveKidentical("iiiiiceooookddeeee", 3);
// N = 2
task.removeConsecutiveKidentical("caaooooodeees", 2);
}
Output
Given text : iiiiiceooookddeeee
Given K : 3
Result : iiceokdde
Given text : caaooooodeees
Given K : 2
Result : codes
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