Chandu and Consecutive Letters

Chandu is very fond of strings. (Or so he thinks!) But, he does not like strings which have same consecutive letters. No one has any idea why it is so. He calls these strings as Bad strings. So, Good strings are the strings which do not have same consecutive letters. Now, the problem is quite simple. Given a string S, you need to convert it into a Good String.

You simply need to perform one operation - if there are two same consecutive letters, delete one of them.

Input:
The first line contains an integer T, denoting the number of test cases.
Each test case consists of a string S, which consists of only lower case letters.

Output:
For each test case, print the answer to the given problem.

Constraints:
1 <= T <= 10
1 <= |S| <= 30

SAMPLE INPUT       SAMPLE OUTPUT
3
abb                  ab
aaab                 ab
ababa                ababa

Explanation
In the first case, S = "abb". Since, S has same consecutive letter 'b' we will delete one of them. So, the good string will be "ab".

In the second case, S = "aaab" and since S has same consecutive letter 'a' we will delete them one by one. aaab -> aab -> ab. So, the good string will be "ab".

In the third case, S = "ababa" and S has no same consecutive letter. So, the good string will be "ababa".

-------------------------------------------------------------------------------
SIMPLE C++ IMPLEMENTATION :
-------------------------------------------------------------------------------

#include <iostream>
using namespace std;
#include<string.h>
void remove(char[],int*,int) ;

int main()
{
    int t,n,i ;
    char ch[30] ;
    cin>>t ;
    while(t--)
    {
    cin>>ch ;
    n=strlen(ch) ;
    for(i=0;i<n-1;i++)
    {
    if(ch[i]==ch[i+1])
    {
remove(ch,&n,i) ;
i-- ;
}
}
    for(i=0;i<n;i++)
    cout<<ch[i] ;
    cout<<endl ;
    }
    return 0;
}

void remove(char ch[],int *no,int k)
{
int i ;
for(i=k;i<*(no);i++)
ch[i]=ch[i+1] ;
(*no)-- ;
}

--------------------------------------------------------------------------------

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )