[LeetCode] 1487. Making File Names Unique

Given an array of strings names of size n. You will create n folders in your file system such that, at the ith minute, you will create a folder with the name names[i].

Since two files cannot have the same name, if you enter a folder name that was previously used, the system will have a suffix addition to its name in the form of (k), where, k is the smallest positive integer such that the obtained name remains unique.

Return an array of strings of length n where ans[i] is the actual name the system will assign to the ith folder when you create it.

Example 1:

Input: names = ["pes","fifa","gta","pes(2019)"]
Output: ["pes","fifa","gta","pes(2019)"]
Explanation: Let's see how the file system creates folder names:
"pes" --> not assigned before, remains "pes"
"fifa" --> not assigned before, remains "fifa"
"gta" --> not assigned before, remains "gta"
"pes(2019)" --> not assigned before, remains "pes(2019)"

Example 2:

Input: names = ["gta","gta(1)","gta","avalon"]
Output: ["gta","gta(1)","gta(2)","avalon"]
Explanation: Let's see how the file system creates folder names:
"gta" --> not assigned before, remains "gta"
"gta(1)" --> not assigned before, remains "gta(1)"
"gta" --> the name is reserved, system adds (k), since "gta(1)" is also reserved, systems put k = 2. it becomes "gta(2)"
"avalon" --> not assigned before, remains "avalon"

Example 3:

Input: names = ["onepiece","onepiece(1)","onepiece(2)","onepiece(3)","onepiece"]
Output: ["onepiece","onepiece(1)","onepiece(2)","onepiece(3)","onepiece(4)"]
Explanation: When the last folder is created, the smallest positive valid k is 4, and it becomes "onepiece(4)".

Constraints:

  • 1 <= names.length <= 5 * 104
  • 1 <= names[i].length <= 20
  • names[i] consists of lowercase English letters, digits, and/or round brackets.

保证文件名唯一。

给你一个长度为 n 的字符串数组 names 。你将会在文件系统中创建 n 个文件夹:在第 i 分钟,新建名为 names[i] 的文件夹。

由于两个文件 不能 共享相同的文件名,因此如果新建文件夹使用的文件名已经被占用,系统会以 (k) 的形式为新文件夹的文件名添加后缀,其中 k 是能保证文件名唯一的 最小正整数 。

返回长度为 n 的字符串数组,其中 ans[i] 是创建第 i 个文件夹时系统分配给该文件夹的实际名称。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/making-file-names-unique
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

题意是往一个系统里面塞文件名,有的文件名是带括号的,有的是不带括号的,请按规则对其进行修改,确保文件名不重复。

这道题理论上不应该是个难题但是有些细节不是很容易一下子就想清楚的。既然是需要做到文件名不重复,那么一定会需要用到 hashmap,key 存的是遇到的文件名,value 是存这个文件名出现的次数。

  • 如果遇到没见过的 key,就存入 hashmap
  • 如果遇到见过的 key,则首先需要看一下当前这个 key 的 value 是多少,记为count,同时看一下是否有类似的文件名已经已另一种方式存入 hashmap 了

打个比方,如果按顺序存了"gta", "gta(1)",如果此时再存一个"gta"的话,如果从hashmap的角度出发,应该将"gta(1)"存入结果集,但是其实是不对的,所以在做存储动作之前需要在 hashmap 里查看是否存在一个由"gta"和map.get("gta")拼接成的key,若存在则一直做 count++ 直到找到一个最小的数字。找到这个数字之后,需要在 hashmap 里存入两个东西,一个是 map.put("gta", map.get("gta") + 1),另一个是拼接成的新的 key,map.put("gta" + "(" + count + ")", 1); 最后结果集存的是"gta" + "(" + count + ")"。

时间O(n)

空间O(n)

Java实现

 1 class Solution {
 2     public String[] getFolderNames(String[] names) {
 3         // corner case
 4         if (names == null || names.length == 0) {
 5             return null;
 6         }
 7         
 8         // normal case
 9         HashMap<String, Integer> map = new HashMap<>();
10         String[] res = new String[names.length];
11         for (int i = 0; i < names.length; i++) {
12             if (!map.containsKey(names[i])) {
13                 map.put(names[i], 1);
14                 res[i] = names[i];
15             } else {
16                 int count = map.get(names[i]);
17                 while (map.containsKey(names[i] + "(" + count + ")")) {
18                     count++;
19                 }
20                 map.put(names[i] + "(" + count + ")", 1);
21                 map.put(names[i], map.get(names[i]) + 1);
22                 res[i] = names[i] + "(" + count + ")";
23             }
24         }
25         return res;
26     }
27 }

 

LeetCode 题目总结

posted @ 2020-06-22 06:20  CNoodle  阅读(579)  评论(0编辑  收藏  举报