From 38e11eb8630c581bfd59d731673ad6b22bef74d7 Mon Sep 17 00:00:00 2001 From: Xevion Date: Thu, 14 Jan 2021 19:30:07 -0600 Subject: [PATCH] making-file-names-unique solution java/regex java python --- making-file-names-unique/Solution.java | 35 ++++++++ making-file-names-unique/Solution.py | 18 ++++ making-file-names-unique/Solution_regex.java | 90 ++++++++++++++++++++ 3 files changed, 143 insertions(+) create mode 100644 making-file-names-unique/Solution.java create mode 100644 making-file-names-unique/Solution.py create mode 100644 making-file-names-unique/Solution_regex.java diff --git a/making-file-names-unique/Solution.java b/making-file-names-unique/Solution.java new file mode 100644 index 0000000..5ed4b60 --- /dev/null +++ b/making-file-names-unique/Solution.java @@ -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 count = new HashMap(); + + 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; + } +} \ No newline at end of file diff --git a/making-file-names-unique/Solution.py b/making-file-names-unique/Solution.py new file mode 100644 index 0000000..3b3a490 --- /dev/null +++ b/making-file-names-unique/Solution.py @@ -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 \ No newline at end of file diff --git a/making-file-names-unique/Solution_regex.java b/making-file-names-unique/Solution_regex.java new file mode 100644 index 0000000..28f99cb --- /dev/null +++ b/making-file-names-unique/Solution_regex.java @@ -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> nameCount = new HashMap>(); + List submit = new ArrayList(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()); + // 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()); + 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()); + nameCount.get(name).add(-1); + submit.add(name); + } + } + } + + return submit.toArray(new String[0]); + } + + public int GetKValue(Set used) { + if (!used.contains(-1)) + return -1; + + for (int i = 1;; i++) { + if (!used.contains(i)) + return i; + } + } +} \ No newline at end of file