Skip to content

Instantly share code, notes, and snippets.

@vrat28
Created May 18, 2021 15:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vrat28/9efe490a35251155305a82a4b14cc6e4 to your computer and use it in GitHub Desktop.
Save vrat28/9efe490a35251155305a82a4b14cc6e4 to your computer and use it in GitHub Desktop.
Find Duplicate File
class Solution {
public List<List<String>> findDuplicate(String[] paths) {
Map<String, List<String>> contMap = new HashMap<>();
StringBuilder pathfile = new StringBuilder();
for (String pStr : paths) {
int i = 0;
pathfile.setLength(0);
while (pStr.charAt(i) != ' ') i++;
pathfile.append(pStr.substring(0,i)).append('/');
int pLen = ++i;
for (int j = i, k = 0; i < pStr.length(); i++)
if (pStr.charAt(i) == '(') {
pathfile.append(pStr.substring(j,i));
k = i + 1;
} else if (pStr.charAt(i) == ')') {
String cont = pStr.substring(k, i);
if (!contMap.containsKey(cont))
contMap.put(cont, new ArrayList<>());
contMap.get(cont).add(pathfile.toString());
j = i + 2;
pathfile.setLength(pLen);
}
}
List<List<String>> ans = new ArrayList<>();
for (List<String> v : contMap.values())
if (v.size() > 1) ans.add(v);
return ans;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment