Modify array of strings by replacing characters repeating in the same or remaining strings
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++
#include <bits/stdc++.h>
using namespace std;
void removeDuplicateCharacters(vector<string> arr)
{
unordered_set< char > cset;
int n = arr.size();
vector<string> out;
for ( auto str : arr) {
string out_curr = "" ;
for ( auto ch : str) {
if (cset.find(ch) != cset.end())
continue ;
out_curr += ch;
cset.insert(ch);
}
if (out_curr.size())
out.push_back(out_curr);
}
for ( int i = 0; i < out.size(); i++) {
cout << out[i] << " " ;
}
}
int main()
{
vector<string> arr
= { "Geeks" , "For" , "Geeks" , "Post" };
removeDuplicateCharacters(arr);
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG{
static void removeDuplicateCharacters(String arr[])
{
HashSet<Character> cset = new HashSet<>();
int n = arr.length;
ArrayList<String> out = new ArrayList<>();
for (String str : arr)
{
String out_curr = "" ;
for ( char ch : str.toCharArray())
{
if (cset.contains(ch))
continue ;
out_curr += ch;
cset.add(ch);
}
if (out_curr.length() != 0 )
out.add(out_curr);
}
for ( int i = 0 ; i < out.size(); i++)
{
System.out.print(out.get(i) + " " );
}
}
public static void main(String[] args)
{
String arr[] = { "Geeks" , "For" , "Geeks" , "Post" };
removeDuplicateCharacters(arr);
}
}
|
Python3
def removeDuplicateCharacters(arr):
cset = set ([])
n = len (arr)
out = []
for st in arr:
out_curr = ""
for ch in st:
if (ch in cset):
continue
out_curr + = ch
cset.add(ch)
if ( len (out_curr)):
out.append(out_curr)
for i in range ( len (out)):
print (out[i], end = " " )
if __name__ = = "__main__" :
arr = [ "Geeks" , "For" , "Geeks" , "Post" ]
removeDuplicateCharacters(arr)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void removeDuplicateCharacters( string [] arr)
{
HashSet< int > cset = new HashSet< int >();
int n = arr.Length;
List< string > Out = new List< string >();
foreach ( string str in arr)
{
string out_curr = "" ;
foreach ( char ch in str.ToCharArray())
{
if (cset.Contains(ch))
continue ;
out_curr += ch;
cset.Add(ch);
}
if (out_curr.Length != 0)
Out.Add(out_curr);
}
for ( int i = 0; i < Out.Count; i++)
{
Console.Write(Out[i] + " " );
}
}
static public void Main (){
string [] arr = { "Geeks" , "For" ,
"Geeks" , "Post" };
removeDuplicateCharacters(arr);
}
}
|
Javascript
<script>
function removeDuplicateCharacters(arr)
{
var cset = new Set();
var n = arr.length;
var out = [];
arr.forEach(str => {
var out_curr = "" ;
str.split( '' ).forEach(ch => {
if (!cset.has(ch))
{
out_curr += ch;
cset.add(ch);
}
});
if (out_curr.size!=0)
out.push(out_curr);
});
for ( var i = 0; i < out.length; i++) {
document.write( out[i] + " " );
}
}
var arr
= [ "Geeks" , "For" , "Geeks" , "Post" ];
removeDuplicateCharacters(arr);
</script>
|
Time Complexity: O(N * M) where M is the length of the longest string in the array.
Auxiliary Space: O(N)
Last Updated :
13 Aug, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...