Skip to content
Related Articles

Related Articles

Modify array of strings by replacing characters repeating in the same or remaining strings
  • Last Updated : 17 Mar, 2021

Given an array of strings arr[] consisting of lowercase and uppercase characters only, the task is to modify the array by removing the characters from the strings which are repeating in the same string or any other string. Print the modified array. 

Examples:

Input: arr[] = {“Geeks”, “For”, “Geeks”}
Output: {“Geks”, “For”}
Explanation:
In arr[0[, ‘e’ occurs twice in the string. Removing a single ‘e’ from the first string modifies “Geeks” to “Geks”. 
In arr[1], all characters are non-repeating. Therefore, the string remains unchanged.
In arr[2], the string is same as arr[0]. Therefore, the complete string is required to be removed.

Input: arr[] = {“Geeks”, “For”, “Geeks”, “Post”}
Output: {“Geks”, “For”, “Pt”}

Approach: Follow the steps to solve the problem :



Below is the implementation of the above approach:

C++




// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to remove duplicate
// characters across the strings
void removeDuplicateCharacters(vector<string> arr)
{
    // Stores distinct characters
    unordered_set<char> cset;
 
    // Size of the array
    int n = arr.size();
 
    // Stores the list of
    // modified strings
    vector<string> out;
 
    // Traverse the array
    for (auto str : arr) {
 
        // Stores the modiifed string
        string out_curr = "";
 
        // Iterate over the characters
        // of the modified string
        for (auto ch : str) {
 
            // If charcter is already present
            if (cset.find(ch) != cset.end())
                continue;
 
            out_curr += ch;
 
            // Insert character into the Set
            cset.insert(ch);
        }
 
        if (out_curr.size())
            out.push_back(out_curr);
    }
 
    // Print the list of modified strings
    for (int i = 0; i < out.size(); i++) {
 
        // Print each string
        cout << out[i] << " ";
    }
}
 
// Driver Code
int main()
{
    // Given array of strings
    vector<string> arr
        = { "Geeks", "For", "Geeks", "Post" };
 
    // Function Call to modify the
    // given array of strings
    removeDuplicateCharacters(arr);
}

Java




// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG{
 
// Function to remove duplicate
// characters across the strings
static void removeDuplicateCharacters(String arr[])
{
     
    // Stores distinct characters
    HashSet<Character> cset = new HashSet<>();
 
    // Size of the array
    int n = arr.length;
 
    // Stores the list of
    // modified strings
    ArrayList<String> out = new ArrayList<>();
 
    // Traverse the array
    for(String str : arr)
    {
         
        // Stores the modiifed string
        String out_curr = "";
 
        // Iterate over the characters
        // of the modified string
        for(char ch : str.toCharArray())
        {
             
            // If charcter is already present
            if (cset.contains(ch))
                continue;
 
            out_curr += ch;
 
            // Insert character into the Set
            cset.add(ch);
        }
 
        if (out_curr.length() != 0)
            out.add(out_curr);
    }
 
    // Print the list of modified strings
    for(int i = 0; i < out.size(); i++)
    {
         
        // Print each string
        System.out.print(out.get(i) + " ");
    }
}
 
// Driver Code
public static void main(String[] args)
{
 
    // Given array of strings
    String arr[] = { "Geeks", "For", "Geeks", "Post" };
 
    // Function Call to modify the
    // given array of strings
    removeDuplicateCharacters(arr);
}
}
 
// This code is contributed by Kingash

Python3




# Python 3 program for the above approach
 
# Function to remove duplicate
# characters across the strings
def removeDuplicateCharacters(arr):
 
    # Stores distinct characters
    cset = set([])
 
    # Size of the array
    n = len(arr)
 
    # Stores the list of
    # modified strings
    out = []
 
    # Traverse the array
    for st in arr:
 
        # Stores the modiifed string
        out_curr = ""
 
        # Iterate over the characters
        # of the modified string
        for ch in st:
 
            # If charcter is already present
            if (ch in cset):
                continue
 
            out_curr += ch
 
            # Insert character into the Set
            cset.add(ch)
 
        if (len(out_curr)):
            out.append(out_curr)
 
    # Print the list of modified strings
    for i in range(len(out)):
 
        # Print each string
        print(out[i], end = " ")
 
# Driver Code
if __name__ == "__main__":
 
    # Given array of strings
    arr = ["Geeks", "For", "Geeks", "Post"]
 
    # Function Call to modify the
    # given array of strings
    removeDuplicateCharacters(arr)
 
    # This code is contributed by ukasp.
Output: 
Geks For Pt

 

Time Complexity: O(N * M) where M is the length of the longest string in the array.
Auxiliary Space: O(N)

 

My Personal Notes arrow_drop_up
Recommended Articles
Page :