给定字符串Str ,任务是删除相似字符的第一个相邻对,直到可以。
注意:删除相邻字符得到一个新的字符串,然后再从新字符串中删除重复相邻并不断重复这个过程,直到所有类似的相邻字符对将被删除。
例子:
Input: str = “keexxllx”
Output: kx
Step 0: Remove ee to get “kxxllx”
Step 1: Remove xx to get “kllx”
Step 2: Remove ll to get “kx”
Input: str = “abbaca”
Output: ca
方法:
在C++中使用string的back()和pop_back()方法STL解决上述问题。迭代字符串中的每个字符,如果相邻字符相同,则使用pop_back()函数删除相邻字符。最后,返回最后一个字符串。
下面是上述方法的实现:
C++
// C++ implementation of the above approach
#include
using namespace std;
// Function to remove adjacent duplicates
string removeDuplicates(string S)
{
string ans = "";
// Iterate for every character
// in the string
for (auto it : S) {
// If ans string is empty or its last
// character does not match with the
// current character then append this
// character to the string
if (ans.empty() or ans.back() != it)
ans.push_back(it);
// Matches with the previous one
else if (ans.back() == it)
ans.pop_back();
}
// Return the answer
return ans;
}
// Driver Code
int main()
{
string str = "keexxllx";
cout << removeDuplicates(str);
}
Java
// Java implementation of the above approach
class GFG
{
// Function to remove adjacent duplicates
static String removeDuplicates(String S)
{
String ans = "";
// Iterate for every character
// in the string
for (int i = 0; i < S.length(); i++)
{
// If ans string is empty or its last
// character does not match with the
// current character then append this
// character to the string
if (ans.isEmpty() ||
ans.charAt(ans.length() - 1) != S.charAt(i))
ans += S.charAt(i);
// Matches with the previous one
else if (ans.charAt(ans.length() - 1) == S.charAt(i))
ans = ans.substring(0, ans.length() - 1);
}
// Return the answer
return ans;
}
// Driver Code
public static void main(String[] args)
{
String str = "keexxllx";
System.out.println(removeDuplicates(str));
}
}
// This code is contributed by
// sanjeev2552
Python3
# Python3 implementation of the above approach
# Function to remove adjacent duplicates
def removeDuplicates(S) :
ans = "";
# Iterate for every character
# in the string
for it in S :
# If ans string is empty or its last
# character does not match with the
# current character then append this
# character to the string
if (ans == "" or ans[-1] != it) :
ans += it ;
# Matches with the previous one
elif (ans[-1] == it) :
ans = ans[:-1];
# Return the answer
return ans;
# Driver Code
if __name__ == "__main__" :
string = "keexxllx";
print(removeDuplicates(string));
# This code is contributed by AnkitRai01
C#
// C# implementation of the above approach
using System;
class GFG
{
// Function to remove adjacent duplicates
static String removeDuplicates(String S)
{
String ans = "";
// Iterate for every character
// in the string
for (int i = 0; i < S.Length; i++)
{
// If ans string is empty or its last
// character does not match with the
// current character then append this
// character to the string
if (ans == "" ||
ans[ans.Length - 1] != S[i])
ans += S[i];
// Matches with the previous one
else if (ans[ans.Length - 1] == S[i])
ans = ans.Substring(0, ans.Length - 1);
}
// Return the answer
return ans;
}
// Driver Code
public static void Main(String[] args)
{
String str = "keexxllx";
Console.WriteLine(removeDuplicates(str));
}
}
// This code is contributed by Rajput-Ji
输出:
kx
想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。