NPLQ19D Bharath and his strings - Editorial

PROBLEM LINK:
https://www.codechef.com/NPLQ2019/problems/NPLQ19D

Practice
Contest

Author: Rishi Gurjar
Editorialist: Rishi Gurjar

DIFFICULTY:
Cakewalk

PROBLEM:
Output the string after removing duplicate occurrences of all characters. Order of characters must be same as input string.

EXPLANATION:
Keep an array A of size 26 to check if we have already encounter some character or not. Initially all values in A are zero. Now when we start traversing the string and if we encounter some character that has not been visited yet, add it to the result and make it visited in array A (make A[s[i]-‘a’]=1 ).

SOLUTIONS:

Setter's solution

#include<bits/stdc++.h>
using namespace std;
int main()
{

int t;
cin>>t;
while(t--)
{
	string s;
	cin>>s;
	int a[26]={0};
	string res="";
	for(int i=0;i<s.length();i++)
	{
		if(a[s[i]-'a']==0)
		{
			a[s[i]-'a']++;
			res+=s[i];
		}
		
	}
	cout<<res<<endl;
}

}