Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

1047. Remove All Adjacent Duplicates In String solution leetcode in c++

#include<iostream>
#include<stack>
using namespace std;
string removeDuplicates(string s) {
	stack<char> ss;
	for (int i = 0; i < s.length(); i++)
	{
		if (ss.empty())
		{
			ss.push(s[i]);
		}
		else
		{
			if (ss.top() == s[i])
			{
				ss.pop();
			}
			else
			{
				ss.push(s[i]);
			}
		}
	}
	string s2;
	while (ss.size())
	{
		s2.push_back(ss.top());
		ss.pop();
	}
	reverse(s2.begin(), s2.end());
	return s2;
}
int main()
{
	string s;
	cin >> s;
	string result = removeDuplicates(s);
	cout << result << "
";
	return 0;
}
Source by www.codegrepper.com #
 
PREVIOUS NEXT
Tagged: #Remove #All #Adjacent #Duplicates #In #String #solution #leetcode
ADD COMMENT
Topic
Name
9+6 =