[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 which is 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)".

Example 4:

Input: names = ["wano","wano","wano","wano"]
Output: ["wano","wano(1)","wano(2)","wano(3)"]
Explanation: Just increase the value of k each time you create folder "wano".

Example 5:

Input: names = ["kaido","kaido(1)","kaido","kaido(1)"]
Output: ["kaido","kaido(1)","kaido(2)","kaido(1)(1)"]
Explanation: Please note that system adds the suffix (k) to current name even it contained the same suffix before.

Constraints:

  • 1 <= names.length <= 5 * 10^4
  • 1 <= names[i].length <= 20
  • names[i] consists of lower case English letters, digits and/or round brackets.

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

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

如果遇到没见过的key,无论带不带括号,都无条件存到hashmap并且value = 1

如果遇到见过的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 题目总结

原文地址:https://www.cnblogs.com/cnoodle/p/13175067.html