Leetcode Problem 1487. Making File Names Unique

1487. Making File Names Unique

Leetcode Solutions

Using HashMap to Track Unique Folder Names

  1. Initialize a HashMap nameCount to keep track of the number of times a name has been used.
  2. Initialize an array result to store the final folder names.
  3. Iterate over the input array names.
  4. For each name, check if it exists in the nameCount HashMap.
  5. If the name does not exist, add it to the nameCount with a count of 1 and append it to result.
  6. If the name exists, increment the count and append (k) to the name, where k is the count.
  7. Check if the new name with (k) also exists. If it does, increment k and repeat until a unique name is found.
  8. Once a unique name is found, add it to the nameCount and result.
  9. Return the result array.
UML Thumbnail

Brute Force Approach with String Manipulation

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...