Solution For Making File Names Unique
Problem Statement:
Given an array of strings files, where each file[i] is formatted as ” {unique_id} {name}.txt”, generate a list of all the files that share the same name and their corresponding paths. For each group of files that share the same name, you must generate a list of their absolute file paths. You may return the answer in any order.
A path is a string that represents the directory tree where a file is located. The format of a path is one or more concatenated strings of the form: ‘/’ followed by one or more lowercase English letters. For example, “/leetcode” and “/leetcode/problems” are valid paths while an empty string “” and “/” are not.
Example:
Input:
files = [“1.txt”,”2.txt”,”3.txt”,”4.txt”,”5.txt”]
Output:
[“1.txt”,”2.txt”,”3.txt”,”4.txt”,”5.txt”]
Explanation: No two files share the same name in the given input files.
Approach:
The given problem can be solved using a hash table. We can use the dictionary to store the file names as keys and the list of corresponding file paths as values. While iterating through the file names, we can split them into unique_id and name. Then, we can check if there are any files with the same name already stored in the dictionary. If yes, we can append the current file path to the corresponding key. If no, we can add a new key-value pair to the dictionary with the name as the key and the current file path as the value.
After iterating through all the files, we can traverse through the dictionary and convert the dictionary values into a list of file paths for each key. Finally, we can return the list of file paths.
Code:
def findDuplicate(paths):
d = {}
for path in paths:
split_list = path.split()
path_str = split_list[0]
for s in split_list[1:]:
name, content = s.split(“(“)
if name not in d:
d[name] = []
d[name].append(path_str + “/” + content[:-1])
return [d[key] for key in d if len(d[key]) > 1]
Step by Step Implementation For Making File Names Unique
//Solution: import java.util.*; public class Main { public static String[] getUniqueFileNames(String[] names) { //Create a HashSet to store unique file names Setset = new HashSet<>(); //Iterate through the input array for(String name : names) { //If the file name is not in the HashSet, add it if(!set.contains(name)) { set.add(name); } //If the file name is already in the HashSet, //create a new file name by appending a number to it else { int i = 1; while(set.contains(name + "(" + i + ")")) { i++; } name = name + "(" + i + ")"; set.add(name); } } //Convert the HashSet back to an array return set.toArray(new String[0]); } public static void main(String[] args) { String[] names = new String[] {"doc", "doc", "image", "doc(1)", "doc"}; String[] uniqueNames = getUniqueFileNames(names); //Print the array of unique file names for(String name : uniqueNames) { System.out.println(name); } } }
def getFolderNames(names): # Create a dictionary to store the count of each name name_count = {} # Create a list to store the unique names unique_names = [] # Iterate through the list of names for name in names: # If the name is not in the dictionary, add it to the dictionary and list with a count of 1 if name not in name_count: name_count[name] = 1 unique_names.append(name) # If the name is in the dictionary, increment the count and create a new name with the count appended else: name_count[name] += 1 new_name = name + "(" + str(name_count[name]) + ")" # Add the new name to the dictionary and list name_count[new_name] = 1 unique_names.append(new_name) return unique_names
var getFolderNames = function(names) { // create a map to store the count of each name let map = {}; // create a set to store the unique names let set = new Set(); // iterate over the input array for (let i = 0; i < names.length; i++) { // if the name is not in the map, add it to the map and the set if (!map[names[i]]) { map[names[i]] = 1; set.add(names[i]); } else { // name is already in the map // create a variable to store the new name let newName = names[i] + "(" + map[names[i]] + ")"; // increment the count for the name in the map map[names[i]]++; // keep incrementing the count and creating new names until a unique name is found while (set.has(newName)) { newName = names[i] + "(" + map[names[i]] + ")"; map[names[i]]++; } // once a unique name is found, add it to the set set.add(newName); } } // return the set as an array return [...set]; };
unordered_mapm; vector getFolderNames(vector & names) { vector res; for (string name : names) { if (m.count(name)) { int k = m[name]++; while (m.count(name + "(" + to_string(k) + ")")) { k++; } name = name + "(" + to_string(k) + ")"; } m[name] = 1; res.push_back(name); } return res; }
using System; using System.Collections.Generic; using System.Linq; public class Solution { public string[] GetUniqueFileNames(string[] names) { // check for empty or null array if (names == null || names.Length == 0) { return new string[0]; } // use a HashSet to track unique file names HashSetuniqueNames = new HashSet (); // loop through each name in the array foreach (string name in names) { // if the name is not in the HashSet, add it if (!uniqueNames.Contains(name)) { uniqueNames.Add(name); } // if the name is in the HashSet, create a new unique name else { // create a new name by appending a number to the end of the name // start the number at 1 int num = 1; // create a new name by appending the number to the end of the name string newName = name + num; // if the new name is not in the HashSet, add it and continue if (!uniqueNames.Contains(newName)) { uniqueNames.Add(newName); continue; } // if the new name is in the HashSet, increment the number and try again else { // increment the number num++; // create a new name by appending the number to the end of the name newName = name + num; } } } // convert the HashSet back to an array and return it return uniqueNames.ToArray(); } }