g1901_2000.s1948_delete_duplicate_folders_in_system.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java11 Show documentation
Show all versions of leetcode-in-java11 Show documentation
Java Solution for LeetCode algorithm problems, continually updating
The newest version!
package g1901_2000.s1948_delete_duplicate_folders_in_system;
// #Hard #Array #String #Hash_Table #Trie #Hash_Function
// #2022_05_18_Time_92_ms_(97.82%)_Space_69.5_MB_(93.45%)
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Solution {
private Map> duplicates;
private List> foldersWithRemovedNames;
public List> deleteDuplicateFolder(List> paths) {
duplicates = new HashMap<>();
Folder rootFolder = new Folder("", null);
for (List path : paths) {
Folder folder = rootFolder;
for (String foldername : path) {
folder = folder.addSubFolder(foldername);
}
}
rootFolder.calculateHash();
for (Map.Entry> entry : duplicates.entrySet()) {
ArrayList foldersWithSameHash = entry.getValue();
if (foldersWithSameHash != null && foldersWithSameHash.size() > 1) {
for (Folder folder : foldersWithSameHash) {
folder.parent.subFolders.remove(folder.name);
}
}
}
foldersWithRemovedNames = new ArrayList<>();
for (Map.Entry entry : rootFolder.subFolders.entrySet()) {
Folder folder = entry.getValue();
List path = new ArrayList<>();
folder.addPaths(path);
}
return foldersWithRemovedNames;
}
private class Folder {
private String name;
private Map subFolders;
private Folder parent;
private String folderHash;
private Folder(String folderName, Folder parentFolder) {
name = folderName;
subFolders = new HashMap<>();
folderHash = "";
parent = parentFolder;
}
private Folder addSubFolder(String foldername) {
return subFolders.computeIfAbsent(foldername, f -> new Folder(f, this));
}
private void calculateHash() {
List subFolderNames = new ArrayList<>(subFolders.keySet());
Collections.sort(subFolderNames);
StringBuilder builder = new StringBuilder();
for (String foldername : subFolderNames) {
Folder folder = subFolders.get(foldername);
folder.calculateHash();
builder.append('#');
builder.append(foldername);
if (folder.folderHash.length() > 0) {
builder.append('(');
builder.append(folder.folderHash);
builder.append(')');
}
}
folderHash = builder.toString();
if (folderHash.length() > 0) {
ArrayList duplicateFolders =
duplicates.computeIfAbsent(folderHash, k -> new ArrayList<>());
duplicateFolders.add(this);
}
}
private void addPaths(List parentPath) {
List currentPath = new ArrayList<>(parentPath);
currentPath.add(name);
foldersWithRemovedNames.add(currentPath);
for (Map.Entry entry : subFolders.entrySet()) {
Folder folder = entry.getValue();
folder.addPaths(currentPath);
}
}
}
}