-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1487. Making File Names Unique (HashMap + Path Compression) 20.6.21 Medium
More file actions
79 lines (64 loc) · 3.19 KB
/
1487. Making File Names Unique (HashMap + Path Compression) 20.6.21 Medium
File metadata and controls
79 lines (64 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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.
Solution: O(n) T and S
'''
If a name conflict exists of course we need to add an int (k),
we can start by trying 1 and incrementing until a we find a suitable k
Once we find k, store this k as the largest suffix for the cooresponding base name (Similar to path compression)
Next time, if we find the same base name, we will automatically start at k + 1
'''
class Solution(object):
def getFolderNames(self, names):
"""
:type names: List[str]
:rtype: List[str]
"""
name_to_curr_k = {}
res = []
for name in names:
if name in name_to_curr_k:
k = name_to_curr_k[name] + 1
while name + "(" + str(k) + ")" in name_to_curr_k: # 之前有基础,才需要更新k
k += 1
name_to_curr_k[name] = k
name = name + "(" + str(k) + ")"
name_to_curr_k[name] = 0 # 没有conflict就存入,不用往前看
res.append(name)
return res