Skip to content

Instantly share code, notes, and snippets.

@ajinkyajawale14499
Created July 7, 2019 09:06
Show Gist options
  • Save ajinkyajawale14499/fcaf3d268b8134b781715f8b60200bbb to your computer and use it in GitHub Desktop.
Save ajinkyajawale14499/fcaf3d268b8134b781715f8b60200bbb to your computer and use it in GitHub Desktop.
Shorten the string which same adjacent characters
#include <iostream>
#include <algorithm>
#include <bits/stdc++.h>
#include <stack>
using namespace std;
// logic is each time character is pushed over stack will be checked with following character if it same continue else print the char
void calculate(string str){
stack<char> s;
for(int i=0;i<str.length();i++)
{
s.push(str.at(i));
if(s.top()=str.at(i+1))
{
s.pop();
continue;
}
else //not equal
{
char x= s.top();
cout<< x <<endl;
s.pop();
}
s.push(str.at(i));
}
}
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int t;
while(t--){
string str;
cin>>str;
calculate(str);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment