从字符串中删除三个连续的重复项
给定一个字符串,您必须从字符串中删除三个连续的重复项。如果没有三个是连续的,则按原样输出字符串。
例子:
Input : aabbbaccddddc
Output :ccdc
Input :aabbaccddc
Output :aabbaccddc
解释 :
我们将字符串字符插入到向量中,并不断检查向量的大小。如果向量的大小大于 2,那么我们将检查字符串的最后 3 个字符是否相同。如果字符相同
然后我们将使用 resize() 在数组中向后移动三步,否则不会。
C++
// C++ program to remove three consecutive
// duplicates
#include
using namespace std;
// function to remove three consecutive
// duplicates
void remove3ConsecutiveDuplicates(string str)
{
vector v;
for (int i = 0; i < str.size(); ++i) {
v.push_back(str[i]);
if (v.size() > 2) {
int sz = v.size();
// removing three consecutive duplicates
if (v[sz - 1] == v[sz - 2] &&
v[sz - 2] == v[sz - 3]) {
v.resize(sz - 3); // Removing three characters
// from the string
}
}
}
// printing the string final string
for (int i = 0; i < v.size(); ++i)
cout << v[i];
}
// driver code
int main()
{
string str = "aabbbaccddddc";
remove3ConsecutiveDuplicates(str);
return 0;
}
Java
// Java program to remove three consecutive
// duplicates
import java.util.*;
class GFG
{
// function to remove three consecutive
// duplicates
static void remove3ConsecutiveDuplicates(String str)
{
Vector v = new Vector<>();
for (int i = 0; i < str.length(); ++i)
{
v.add(str.charAt(i));
if (v.size() > 2)
{
int sz = v.size();
// removing three consecutive duplicates
if (v.get(sz - 1) == v.get(sz - 2) &&
v.get(sz - 2) == v.get(sz - 3))
{
v.setSize(sz - 3); // Removing three characters
// from the string
}
}
}
// printing the string final string
for (int i = 0; i < v.size(); ++i)
System.out.print(v.get(i));
}
// Driver code
public static void main(String[] args)
{
String str = "aabbbaccddddc";
remove3ConsecutiveDuplicates(str);
}
}
// This code contributed by Rajput-Ji
Python3
# Python3 program to remove three consecutive duplicates
# function to remove three consecutive duplicates
def remove3ConsecutiveDuplicates(string):
val = ""
i = 0
while (i < len(string)):
if (i < len(string) - 2 and
string[i] * 3 == string[i:i + 3]):
i += 3
else:
val += string[i]
i += 1
if (len(val) == len(string)):
return val
else:
return remove3ConsecutiveDuplicates(val)
# Driver code
string = "aabbbaccddddc"
val = remove3ConsecutiveDuplicates(string)
print(val)
# This code is contributed by
# Shubham Singh(SHUBHAMSINGH10)
C#
// C# program to remove three consecutive
// duplicates
using System;
using System.Collections.Generic;
class GFG
{
// function to remove three consecutive
// duplicates
static void remove3ConsecutiveDuplicates(String str)
{
List v = new List();
for (int i = 0; i < str.Length; ++i)
{
v.Add(str[i]);
if (v.Count > 2)
{
int sz = v.Count;
// removing three consecutive duplicates
if (v[sz - 1] == v[sz - 2] &&
v[sz - 2] == v[sz - 3])
{
v.RemoveRange(sz-3,3); // Removing three characters
// from the string
}
}
}
// printing the string final string
for (int i = 0; i < v.Count; ++i)
Console.Write(v[i]);
}
// Driver code
public static void Main(String[] args)
{
String str = "aabbbaccddddc";
remove3ConsecutiveDuplicates(str);
}
}
// This code has been contributed by 29AjayKumar
PHP
2)
{
$sz = count($v);
// removing three consecutive duplicates
if ($v[$sz - 1] == $v[$sz - 2] &&
$v[$sz - 2] == $v[$sz - 3])
{
array_pop($v);
array_pop($v);
array_pop($v);
// Removing three characters
// from the string
}
}
}
// printing the string final string
for ($i = 0; $i < count($v); ++$i)
echo $v[$i];
}
// Driver code
$str = "aabbbaccddddc";
remove3ConsecutiveDuplicates($str);
// This code is contributed by mits
?>
Javascript
输出:
ccdc