Given a string s, remove all duplicate characters from the string. Make sure relative order of characters are preserved.
#include<iostream>
using namespace std;
int main()
{
char s[255];
int hash[255] = {0};
cin>>s;
int n = strlen(s);
for(int i = 0; i < n; i++)
{
hash[s[i]]++;
}
int j = 0;
for(int i =0; i < n; i++)
{
if(hash[s[i]] > 0)
{
s[j] = s[i];
hash[s[i]] = 0;
j++;
}
}
s[j] = '\0';
cout<<s;
return 0;
}
No comments:
Post a Comment