给定的阵列ARR []由N字符串,任务是由通过附加AY数,使得阵列中的所有字符串是唯一替换重复的字符串来修改阵列。
例子:
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”}
方法:想法是遍历数组并将每个字符串的频率存储在Hashmap中的arr []中。在存储频率时,如果字符串以前没有出现过,则保持字符串不变。否则,请在末尾附加其频率。最后,打印数组arr []中存在的所有唯一字符串。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to replace duplicate strings
// by alphanumeric strings to make all
// strings in the array unique
void replaceDuplicates(
vector& names)
{
// Store the frequency of strings
unordered_map hash;
// Iterate over the array
for (int i = 0;
i < names.size(); i++) {
// For the fist occurrence,
// update the frequency count
if (hash.count(names[i]) == 0)
hash[names[i]]++;
// Otherwise
else {
int count = hash[names[i]]++;
// Append frequency count
// to end of the string
names[i] += to_string(count);
}
}
// Print the modified array
for (int i = 0;
i < names.size(); i++) {
cout << names[i] << " ";
}
}
// Driver Code
int main()
{
vector str
= { "aa", "bb", "cc", "bb",
"aa", "aa", "aa" };
// Function Call
replaceDuplicates(str);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG{
// Function to replace duplicate strings
// by alphanumeric strings to make all
// strings in the array unique
static void replaceDuplicates(String[] names)
{
// Store the frequency of strings
HashMap hash = new HashMap<>();
// Iterate over the array
for(int i = 0; i < names.length; i++)
{
// For the fist occurrence,
// update the frequency count
if (!hash.containsKey(names[i]))
hash.put(names[i], 1);
// Otherwise
else
{
int count = hash.get(names[i]);
hash.put(names[i], hash.get(names[i]) + 1);
// Append frequency count
// to end of the string
names[i] += Integer.toString(count);
}
}
// Print the modified array
for(int i = 0; i < names.length; i++)
{
System.out.print(names[i] + ' ');
}
}
// Driver Code
public static void main(String[] args)
{
String[] str = { "aa", "bb", "cc",
"bb", "aa", "aa", "aa" };
// Function Call
replaceDuplicates(str);
}
}
// This code is contributed by akhilsaini
Python3
# Python3 program for the above approach
# Function to replace duplicate strings
# by alphanumeric strings to make all
# strings in the array unique
def replaceDuplicates(names):
# Store the frequency of strings
hash = {}
# Iterate over the array
for i in range(0, len(names)):
# For the fist occurrence,
# update the frequency count
if names[i] not in hash:
hash[names[i]] = 1
# Otherwise
else:
count = hash[names[i]]
hash[names[i]] += 1
# Append frequency count
# to end of the string
names[i] += str(count)
# Print the modified array
for i in range(0, len(names)):
print(names[i], end = ' ')
# Driver Code
if __name__ == '__main__':
str1 = [ "aa", "bb", "cc",
"bb", "aa", "aa", "aa" ]
# Function Call
replaceDuplicates(str1)
# This code is contributed by akhilsaini
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to replace duplicate strings
// by alphanumeric strings to make all
// strings in the array unique
static void replaceDuplicates(string[] names)
{
// Store the frequency of strings
Dictionary hash = new Dictionary();
// Iterate over the array
for(int i = 0; i < names.Length; i++)
{
// For the fist occurrence,
// update the frequency count
if (!hash.ContainsKey(names[i]))
hash[names[i]] = 1;
// Otherwise
else
{
int count = hash[names[i]];
hash[names[i]] += 1;
// Append frequency count
// to end of the string
names[i] += count.ToString();
}
}
// Print the modified array
for(int i = 0; i < names.Length; i++)
{
Console.Write(names[i] + ' ');
}
}
// Driver Code
public static void Main()
{
string[] str = { "aa", "bb", "cc",
"bb", "aa", "aa", "aa" };
// Function Call
replaceDuplicates(str);
}
}
// This code is contributed by akhilsaini
输出:
aa bb cc bb1 aa1 aa2 aa3
时间复杂度: O(N)
辅助空间: O(N)