mirror of
https://github.com/Xevion/leetcode.git
synced 2025-12-06 17:15:29 -06:00
making-file-names-unique solution java/regex java python
This commit is contained in:
35
making-file-names-unique/Solution.java
Normal file
35
making-file-names-unique/Solution.java
Normal file
@@ -0,0 +1,35 @@
|
||||
// Accepted
|
||||
// Runtime: 41 ms
|
||||
// Memory Usage: 54.7 MB
|
||||
// Submitted: January 14th, 2021
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
class Solution {
|
||||
HashMap<String, Integer> count = new HashMap<String, Integer>();
|
||||
|
||||
public String[] getFolderNames(String[] names) {
|
||||
String[] results = new String[names.length];
|
||||
|
||||
for (int i = 0; i < names.length; i++) {
|
||||
if (count.containsKey(names[i])) {
|
||||
int k = count.get(names[i]) + 1;
|
||||
String test;
|
||||
|
||||
do {
|
||||
test = names[i] + "(" + k++ + ")";
|
||||
} while (count.containsKey(test));
|
||||
|
||||
count.put(test, 0);
|
||||
count.put(names[i], k - 1);
|
||||
results[i] = test;
|
||||
} else {
|
||||
count.put(names[i], 0);
|
||||
results[i] = names[i];
|
||||
}
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
}
|
||||
18
making-file-names-unique/Solution.py
Normal file
18
making-file-names-unique/Solution.py
Normal file
@@ -0,0 +1,18 @@
|
||||
# Accepted
|
||||
# Runtime: 7932 ms
|
||||
# Memory Usage: 26.5 MB
|
||||
# Submitted: January 14th, 2021
|
||||
|
||||
class Solution:
|
||||
def getFolderNames(self, names: List[str]) -> List[str]:
|
||||
result = []
|
||||
names_used = set()
|
||||
for name in names:
|
||||
counter = 0
|
||||
test_name = name
|
||||
while test_name in names_used:
|
||||
counter += 1
|
||||
test_name = f'{name}({counter})'
|
||||
names_used.add(test_name)
|
||||
result.append(test_name)
|
||||
return result
|
||||
90
making-file-names-unique/Solution_regex.java
Normal file
90
making-file-names-unique/Solution_regex.java
Normal file
@@ -0,0 +1,90 @@
|
||||
// Accepted
|
||||
// Runtime: 549 ms
|
||||
// Memory Usage: 52.8 MB
|
||||
// Submitted: January 14th, 2021
|
||||
// Notes: This solution was pretty bad. I didn't understand how the problem could be solved and made a needlessly more complex and specific solution
|
||||
// that worked, but it also used RegEx, did operations it didn't need to, and overall was much too complex and aggressive to work well.
|
||||
// The correct solution was much simpler and more concise. It did technically work, though!
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
class Solution {
|
||||
public String[] getFolderNames(String[] names) {
|
||||
Map<String, Set<Integer>> nameCount = new HashMap<String, Set<Integer>>();
|
||||
List<String> submit = new ArrayList<String>(names.length);
|
||||
Pattern p = Pattern.compile("(.+)\\((\\d+)\\)");
|
||||
|
||||
for (int i = 0; i < names.length; i++) {
|
||||
String name = names[i];
|
||||
|
||||
Matcher m = p.matcher(name);
|
||||
// The filename provided has a k-value suffix
|
||||
if (m.matches()) {
|
||||
String strippedName = m.group(1);
|
||||
int k = Integer.parseInt(m.group(2));
|
||||
|
||||
// If the stripped name has already been seen
|
||||
if (nameCount.containsKey(strippedName)) {
|
||||
// If the k-value has already been recorded
|
||||
if (nameCount.get(strippedName).contains(k)) {
|
||||
// Name has been seen before
|
||||
if (nameCount.containsKey(name)) {
|
||||
// Get best K value, put new K value in, submit and construct new name
|
||||
k = GetKValue(nameCount.get(name));
|
||||
if (k == -1) {
|
||||
nameCount.get(name).add(-1);
|
||||
k = GetKValue(nameCount.get(name));
|
||||
}
|
||||
nameCount.get(name).add(k);
|
||||
submit.add(name + "(" + k + ")");
|
||||
} else {
|
||||
nameCount.put(name, new HashSet<Integer>());
|
||||
// We add -1 and skip to 1 immediately as this is a sort of special entry
|
||||
nameCount.get(name).add(-1);
|
||||
nameCount.get(name).add(1);
|
||||
submit.add(name + "(1)");
|
||||
}
|
||||
} else {
|
||||
// It hasn't been recorded before
|
||||
nameCount.get(strippedName).add(k);
|
||||
submit.add(name);
|
||||
}
|
||||
} else {
|
||||
// Stripped name has never been seen before
|
||||
nameCount.put(strippedName, new HashSet<Integer>());
|
||||
nameCount.get(strippedName).add(k);
|
||||
submit.add(name);
|
||||
}
|
||||
} else {
|
||||
// Name has been seen before
|
||||
if (nameCount.containsKey(name)) {
|
||||
// Get best K value, put new K value in, submit and construct new name
|
||||
int k = GetKValue(nameCount.get(name));
|
||||
nameCount.get(name).add(k);
|
||||
if (k == -1) {
|
||||
submit.add(name);
|
||||
} else {
|
||||
submit.add(name + "(" + k + ")");
|
||||
}
|
||||
} else {
|
||||
nameCount.put(name, new HashSet<Integer>());
|
||||
nameCount.get(name).add(-1);
|
||||
submit.add(name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return submit.toArray(new String[0]);
|
||||
}
|
||||
|
||||
public int GetKValue(Set<Integer> used) {
|
||||
if (!used.contains(-1))
|
||||
return -1;
|
||||
|
||||
for (int i = 1;; i++) {
|
||||
if (!used.contains(i))
|
||||
return i;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user