Wednesday, March 16, 2022

print all the duplicates in the input string gfg practice

Remove all duplicates from a given string 

Easy 

Problem Statement:

Given a string Str which may contains lowercase and uppercase chracters. The task is to remove all duplicate characters from the string and find the resultant string. The order of remaining characters in the output should be same as in the original string.

Example 1:

Input:
Str = geeksforgeeks
Output: geksfor
Explanation: After removing duplicate
characters such as e, k, g, s, we have
string as "geksfor".

Example 2:

Input:
Str = HappyNewYear
Output: HapyNewYr
Explanation: After removing duplicate
characters such as p, e, a, we have
string as "HapyNewYr".

Your Task:
Complete the function removeDuplicates() which takes a string str, as input parameters and returns a string denoting the answer. You don't to print answer or take inputs.

Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)

Constraints:
1 ≤ N ≤ 105
String contains uppercase and lowercase english letters.

Practice here

Remove all duplicates from a given string | Practice | GeeksforGeeks

Solution:


class Solution{

public:

string removeDuplicates(string str) {

    // code here

    unordered_set<char> set;

    string s;

    for(char c: str)

    {   if(set.find(c)==set.end())

        {

        s+=c;   

        }

        set.insert(c);

    }

    return s;

}

};

No comments:

Implement stsStr Leetcode solution

  28.   Implement strStr() Easy Implement  strStr() . Given two strings  needle  and  haystack , return the index of the first occurrence of...