Append digits to the end of duplicate strings to make all strings in an array unique
Given an array arr[] consisting of N strings, the task is to modify the array by replacing the duplicate strings by appending any number such that all the strings in the array are unique.
Examples:
Input: S = {“aa”, “bb”, “cc”, “bb”, “aa”, “aa”, “aa”}
Output: {“aa”, “bb”, “cc”, “bb1”, “aa1”, “aa2”, “aa3”}
Explanation:
The output of the second occurrence of “bb” is “bb1”
The output of the second occurrence of “aa” is “aa1”
The output of the third occurrence of “aa” is “aa2”
The output of the fourth occurrence of “aa” is “aa3”
Input: S = {“aa”, “bb”, “cc”, “aa”}
Output: {“aa”, “bb”, “cc”, “aa1”}
Approach: The idea is to traverse the array and store the frequency of each string in arr[] in a Hashmap. While storing the frequency, if the string has no previous occurrences, then leave the string unchanged. Otherwise, append its frequency at the end. Finally, print all the unique strings present in the array arr[].
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void replaceDuplicates(
vector<string>& names)
{
unordered_map<string, int > hash;
for ( int i = 0;
i < names.size(); i++) {
if (hash.count(names[i]) == 0)
hash[names[i]]++;
else {
int count = hash[names[i]]++;
names[i] += to_string(count);
}
}
for ( int i = 0;
i < names.size(); i++) {
cout << names[i] << " " ;
}
}
int main()
{
vector<string> str
= { "aa" , "bb" , "cc" , "bb" ,
"aa" , "aa" , "aa" };
replaceDuplicates(str);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG{
static void replaceDuplicates(String[] names)
{
HashMap<String, Integer> hash = new HashMap<>();
for ( int i = 0 ; i < names.length; i++)
{
if (!hash.containsKey(names[i]))
hash.put(names[i], 1 );
else
{
int count = hash.get(names[i]);
hash.put(names[i], hash.get(names[i]) + 1 );
names[i] += Integer.toString(count);
}
}
for ( int i = 0 ; i < names.length; i++)
{
System.out.print(names[i] + ' ' );
}
}
public static void main(String[] args)
{
String[] str = { "aa" , "bb" , "cc" ,
"bb" , "aa" , "aa" , "aa" };
replaceDuplicates(str);
}
}
|
Python3
def replaceDuplicates(names):
hash = {}
for i in range ( 0 , len (names)):
if names[i] not in hash :
hash [names[i]] = 1
else :
count = hash [names[i]]
hash [names[i]] + = 1
names[i] + = str (count)
for i in range ( 0 , len (names)):
print (names[i], end = ' ' )
if __name__ = = '__main__' :
str1 = [ "aa" , "bb" , "cc" ,
"bb" , "aa" , "aa" , "aa" ]
replaceDuplicates(str1)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void replaceDuplicates( string [] names)
{
Dictionary< string ,
int > hash = new Dictionary< string ,
int >();
for ( int i = 0; i < names.Length; i++)
{
if (!hash.ContainsKey(names[i]))
hash[names[i]] = 1;
else
{
int count = hash[names[i]];
hash[names[i]] += 1;
names[i] += count.ToString();
}
}
for ( int i = 0; i < names.Length; i++)
{
Console.Write(names[i] + ' ' );
}
}
public static void Main()
{
string [] str = { "aa" , "bb" , "cc" ,
"bb" , "aa" , "aa" , "aa" };
replaceDuplicates(str);
}
}
|
Javascript
<script>
function replaceDuplicates( names)
{
var hash = new Map();
for ( var i = 0;
i < names.length; i++) {
if (!hash.has(names[i]))
hash.set(names[i],1);
else {
var count = hash.get(names[i]);
hash.set(names[i],hash.get(names[i])+1);
names[i] += count.toString();
}
}
for ( var i = 0;
i < names.length; i++) {
document.write( names[i] + " " );
}
}
var str
= [ "aa" , "bb" , "cc" , "bb" ,
"aa" , "aa" , "aa" ];
replaceDuplicates(str);
</script>
|
Output:
aa bb cc bb1 aa1 aa2 aa3
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
21 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...