Remove Duplicates

Given a string S. Your task is to remove all duplicates characters from the string S

NOTE:
1.) The order of characters in the output string should be the same as given in the input string.
2.) String S contains only lowercase characters ['a'-'z'].

Example:

Input:  s = "iloveprogramming"
Output: iloveprgamn

Approach

C++

#include <bits/stdc++.h>
using namespace std;

string removeDuplicates(string s)
{

    set<charst;
    int l = 0;
    for (int i = 0i < s.size(); i++)
    {
        if (st.find(s[i]== st.end())
        {
            s[l++] = s[i];
        }
        st.insert(s[i]);
    }
    s.resize(l);
    return s;
}
int main()
{
    string s = "iloveprogramming";

    cout << removeDuplicates(s<< "\n";

    return 0;
}


No comments:

Post a Comment