Created
May 18, 2021 15:24
-
-
Save vrat28/9efe490a35251155305a82a4b14cc6e4 to your computer and use it in GitHub Desktop.
Find Duplicate File
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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