📌  相关文章
📜  将数字附加到重复字符串的末尾以使数组中的所有字符串都是唯一的

📅  最后修改于: 2021-10-27 09:23:02             🧑  作者: Mango

给定一个由N 个字符串组成的数组arr[] ,任务是通过附加 y 数来替换重复的字符串来修改数组,使得数组中的所有字符串都是唯一的。

例子:

做法:思路是遍历数组,将arr[]中每个字符串的频率存入一个Hashmap中。在存储频率时,如果该字符串之前没有出现过,则保持该字符串不变。否则,在末尾附加它的频率。最后,打印数组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 first 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 first 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 first 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 first 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


Javascript


输出:
aa bb cc bb1 aa1 aa2 aa3

时间复杂度: O(N)
辅助空间: O(N)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程