C++ Remove Duplicates From String














































C++ Remove Duplicates From String



.

Remove duplicates from a string in O(1) extra space


Given a string str of lowercase characters, the task is to remove duplicates and return a resultant string without modifying the order of characters in the original string.

Examples:  

Input: str = geeksforgeeks
Output: geksfor

Input: str = characters
Output: chartes

Approach: The idea is to use bits of a counter variable to mark the presence of a character in the string. To mark the presence of a set 0th bit as 1, for b set 1st bit as 1 and so on. If the corresponding bit of character present in the original string is set to 0, it means it is the first occurrence of that character, hence set its corresponding bit as 1 and keep on including the current character in the resultant string.

Consider the string str = geeksforgeeks

  • character: g
    x = 6(ascii of g  97) 
    6th bit in counter is unset resulting first occurrence of character g. 
    str[0] = g
    counter = 00000000000000000000000001000000 // mark 6th bit as visited 
    length = 1
  • character: e
    x = 4(ascii of e 97) 
    4th bit in counter is unset resulting in first occurrence of character e 
    str[1] = e 
    counter = 00000000000000000000000001010000 //mark 4th bit as visited 
    length = 2
  • character: e
    x = 4(ascii of e  97) 
    4th bit in counter is set resulting in duplicate character. 
    Ignore this character. Move for next character. 
    counter = 00000000000000000000000001010000 //same as previous 
    length = 2
  • character: k 
    x = 10(ascii of k  97) 
    10th bit in counter is unset resulting in first occurrence of character k. 
    str[2] = k 
    counter = 00000000000000000000010001010000 //mark 10th bit as visited 
    length = 3

Similarly, do the same for all characters.
Resultant string : geksfor(string of length 7 starting from index 0) 

Algorithm: 

  1. Initialize a counter variable (keeps track of the characters visited in string), it is a 32 bit Integer represented as(00000000000000000000000000000000) initially.
  2. Consider a as 0th bit of counter, b as 1st bit of counter, c  as 2nd bit of counter and so on.
  3. Traverse through each character of input string.
  4. Get the character s value, where character  s value(x) = Ascii of character  97. This will make sure for value of  a  a   s 0, value of  b  as 1 and so on.
  5. Check xth bit of counter.
  6. If Xth bit of counter is 0 which means the current character has appeared for the first time, keep the current character at the index  length  of string .
  7. Mark the current character visited by setting xth bit of counter.
  8. Increment length.
  9. Return Substring of size  length  from index 0.

Below is the implementation of above approach: 

// C++ implementation of above approach
#include <bits/stdc++.h>
#include <string>
using namespace std;

// Function to remove duplicates
string removeDuplicatesFromString(string str)
{

    // keeps track of visited characters
    int counter = 0;

    int i = 0;
    int size = str.size();

    // gets character value
    int x;

    // keeps track of length of resultant string
    int length = 0;

    while (i < size) {
        x = str[i] - 97;

        // check if Xth bit of counter is unset
        if ((counter & (1 << x)) == 0) {

            str[length] = 'a' + x;

            // mark current character as visited
            counter = counter | (1 << x);

            length++;
        }
        i++;
    }

    return str.substr(0, length);
}

// Driver code
int main()
{
    string str = "geeksforgeeks";
    cout << removeDuplicatesFromString(str);
    return 0;
}


Output: 
geksfor

Time Complexity: O(n) 
Space Complexity: O(n) -> As it uses char[] array of string to store characters of string (i.e. dependent upon length of input string)



More Articles of M Mounika:

Name Views Likes
C++ Inplace Rotate square matrix by 90 degrees 788 0
C++ Introduction to Greedy Approach 2517 2
C++ Gold Mine Problem 1294 0
C++ Anagram substring Search 584 0
C++ Segment Trees 819 2
Disjoint Data Structures 597 1
C++ Greedy Approach-Activity Selection Problem 2806 1
C++ Trie Datastructure 3152 1
C++ Minimum Removal of Characters from string to make its permutation as palindrome 594 0
C++ Greedy Approach Minimum number of coins 1731 0
C++ Greedy Approach-Huffman Codes 5545 1
C++ Manachars Algorithm 1763 0
C++ smallest substring with maximum distinct characters 1436 0
C++ Trie Cost of Data 1186 1
C++ Greedy Approach Maximum product Subset 1006 1
C++ Greedy Approach Maximum height Pyramid 1022 1
C++ Greedy Approach-Egyptian Fractions 1456 0
C++ String Matching(KMP Algorithm) 996 0
C++ K Centers Problem 1240 0
C++ Find Non Repeating Elements in Array 1070 0
C++ Greedy Approach Minimum Product Subset 1170 1
C++ Merge K Sorted Arrays 662 0
C++ Number of Bracket Reversals needed to make expression Balanced 322 0
C++ Basics of String Manipulation 411 1
C++ Problem On Disjoint Data Structures 466 0
C++ Find Nth Catalan Number 1007 0
C++ Greedy Approach String lexicographically largest subsequence 1130 0
C++ Merge Two Binary Trees 705 0
C++ Remove Duplicates From String 3402 0
C++ Minimum characters added at front of string in palindrome conversion 413 0
Disjoint Data Structures Part2 420 0
C++ Greedy Approach Prims MST 1146 1
C++ Greedy Approach N-array maximum sum 776 1
C++ String Matching(Naive Algorithm) 1816 0
C++ Flood Fill Algorithm 2833 0
C++ WildCard pattern matching 2658 0
C++ String Matching(Z Algorithm) 640 0
C++ Meta Strings 685 0
C++ Sum of Numbers From Root To Leaf Paths 675 0
C++ Greedy Approach Lexicographically largest subsequence 902 0
C++ Disjoint Data Structures Part3 377 0
C++ Sieve Of Erastosthenes 534 0
C++ String to Palindrome with Append Function 579 0
C++ Search String in Grid 1583 0
C++ Greedy Approach Policemen Catch Thieves 2047 1
C++ Smallest window with all characters in string 901 0
C++ Greedy Approach Fractional Knapsack 2852 1
C++ Disjoint Data Structure Cycle Detection 569 0
C++ Segmented Sieve (Print Primes In a Range) 1940 0
C++ Greedy Approach Krushkals MST 946 1

Comments