class Solution {
public:
vector<string> getFolderNames(vector<string>& names) {
}
};
1487. 保证文件名唯一
给你一个长度为 n
的字符串数组 names
。你将会在文件系统中创建 n
个文件夹:在第 i
分钟,新建名为 names[i]
的文件夹。
由于两个文件 不能 共享相同的文件名,因此如果新建文件夹使用的文件名已经被占用,系统会以 (k)
的形式为新文件夹的文件名添加后缀,其中 k
是能保证文件名唯一的 最小正整数 。
返回长度为 n
的字符串数组,其中 ans[i]
是创建第 i
个文件夹时系统分配给该文件夹的实际名称。
示例 1:
输入:names = ["pes","fifa","gta","pes(2019)"] 输出:["pes","fifa","gta","pes(2019)"] 解释:文件系统将会这样创建文件名: "pes" --> 之前未分配,仍为 "pes" "fifa" --> 之前未分配,仍为 "fifa" "gta" --> 之前未分配,仍为 "gta" "pes(2019)" --> 之前未分配,仍为 "pes(2019)"
示例 2:
输入:names = ["gta","gta(1)","gta","avalon"] 输出:["gta","gta(1)","gta(2)","avalon"] 解释:文件系统将会这样创建文件名: "gta" --> 之前未分配,仍为 "gta" "gta(1)" --> 之前未分配,仍为 "gta(1)" "gta" --> 文件名被占用,系统为该名称添加后缀 (k),由于 "gta(1)" 也被占用,所以 k = 2 。实际创建的文件名为 "gta(2)" 。 "avalon" --> 之前未分配,仍为 "avalon"
示例 3:
输入:names = ["onepiece","onepiece(1)","onepiece(2)","onepiece(3)","onepiece"] 输出:["onepiece","onepiece(1)","onepiece(2)","onepiece(3)","onepiece(4)"] 解释:当创建最后一个文件夹时,最小的正有效 k 为 4 ,文件名变为 "onepiece(4)"。
示例 4:
输入:names = ["wano","wano","wano","wano"] 输出:["wano","wano(1)","wano(2)","wano(3)"] 解释:每次创建文件夹 "wano" 时,只需增加后缀中 k 的值即可。
示例 5:
输入:names = ["kaido","kaido(1)","kaido","kaido(1)"] 输出:["kaido","kaido(1)","kaido(2)","kaido(1)(1)"] 解释:注意,如果含后缀文件名被占用,那么系统也会按规则在名称后添加新的后缀 (k) 。
提示:
1 <= names.length <= 5 * 10^4
1 <= names[i].length <= 20
names[i]
由小写英文字母、数字和/或圆括号组成。原站题解
cpp 解法, 执行用时: 240 ms, 内存消耗: 57.2 MB, 提交时间: 2023-03-03 09:11:57
class Solution { public: vector<string> getFolderNames(vector<string>& names) { unordered_map<string, int> d; for (auto& name : names) { int k = d[name]; if (k) { while (d[name + "(" + to_string(k) + ")"]) { k++; } d[name] = k; name += "(" + to_string(k) + ")"; } d[name] = 1; } return names; } };
java 解法, 执行用时: 67 ms, 内存消耗: 53.8 MB, 提交时间: 2023-03-03 09:11:24
class Solution { public String[] getFolderNames(String[] names) { Map<String, Integer> index = new HashMap<String, Integer>(); int n = names.length; String[] res = new String[n]; for (int i = 0; i < n; i++) { String name = names[i]; if (!index.containsKey(name)) { res[i] = name; index.put(name, 1); } else { int k = index.get(name); while (index.containsKey(addSuffix(name, k))) { k++; } res[i] = addSuffix(name, k); index.put(name, k + 1); index.put(addSuffix(name, k), 1); } } return res; } public String addSuffix(String name, int k) { return name + "(" + k + ")"; } }
javascript 解法, 执行用时: 212 ms, 内存消耗: 63.2 MB, 提交时间: 2023-03-03 09:11:08
/** * @param {string[]} names * @return {string[]} */ var getFolderNames = function(names) { const index = new Map(); const n = names.length; const res = new Array(n).fill(0); for (let i = 0; i < n; i++) { const name = names[i]; if (!index.has(name)) { res[i] = name; index.set(name, 1); } else { let k = index.get(name); while (index.has(addSuffix(name, k))) { k++; } res[i] = addSuffix(name, k); index.set(name, k + 1); index.set(addSuffix(name, k), 1); } } return res; } const addSuffix = (name, k) => { return name + "(" + k + ")"; };
golang 解法, 执行用时: 104 ms, 内存消耗: 11.9 MB, 提交时间: 2023-03-03 09:10:52
func getFolderNames(names []string) []string { ans := make([]string, len(names)) index := map[string]int{} for p, name := range names { i := index[name] if i == 0 { index[name] = 1 ans[p] = name continue } for index[name+"("+strconv.Itoa(i)+")"] > 0 { i++ } t := name + "(" + strconv.Itoa(i) + ")" ans[p] = t index[name] = i + 1 index[t] = 1 } return ans }
python3 解法, 执行用时: 132 ms, 内存消耗: 31.4 MB, 提交时间: 2023-03-03 09:10:36
class Solution: def getFolderNames(self, names: List[str]) -> List[str]: ans = [] index = {} for name in names: if name not in index: ans.append(name) index[name] = 1 else: k = index[name] while name + '(' + str(k) + ')' in index: k += 1 t = name + '(' + str(k) + ')' ans.append(t) index[name] = k + 1 index[t] = 1 return ans