All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g1901_2000.s1948_delete_duplicate_folders_in_system.Solution.kt Maven / Gradle / Ivy

There is a newer version: 1.32
Show newest version
package g1901_2000.s1948_delete_duplicate_folders_in_system

// #Hard #Array #String #Hash_Table #Trie #Hash_Function
// #2023_06_20_Time_1420_ms_(100.00%)_Space_80.6_MB_(100.00%)

class Solution {
    private var duplicates: MutableMap>? = null
    private var foldersWithRemovedNames: MutableList>? = null

    fun deleteDuplicateFolder(paths: List>): List> {
        duplicates = HashMap()
        val rootFolder = Folder("", null)
        for (path in paths) {
            var folder = rootFolder
            for (foldername in path) {
                folder = folder.addSubFolder(foldername)
            }
        }
        rootFolder.calculateHash()
        for ((_, foldersWithSameHash) in duplicates as HashMap>) {
            if (foldersWithSameHash.size > 1) {
                for (folder in foldersWithSameHash) {
                    folder.parent?.subFolders?.remove(folder.name)
                }
            }
        }
        foldersWithRemovedNames = ArrayList()
        for ((_, folder) in rootFolder.subFolders) {
            val path: List = ArrayList()
            folder.addPaths(path)
        }
        return foldersWithRemovedNames as ArrayList>
    }

    private inner class Folder(val name: String, val parent: Folder?) {
        val subFolders: MutableMap
        private var folderHash = ""

        init {
            subFolders = HashMap()
        }

        fun addSubFolder(foldername: String): Folder {
            return subFolders.computeIfAbsent(foldername) { f: String -> Folder(f, this) }
        }

        fun calculateHash() {
            val subFolderNames: MutableList = ArrayList(subFolders.keys)
            subFolderNames.sort()
            val builder = StringBuilder()
            for (foldername in subFolderNames) {
                val folder = subFolders[foldername]
                folder!!.calculateHash()
                builder.append('#')
                builder.append(foldername)
                if (folder.folderHash.isNotEmpty()) {
                    builder.append('(')
                    builder.append(folder.folderHash)
                    builder.append(')')
                }
            }
            folderHash = builder.toString()
            if (folderHash.isNotEmpty()) {
                val duplicateFolders = duplicates!!.computeIfAbsent(folderHash) { _: String? -> ArrayList() }
                duplicateFolders.add(this)
            }
        }

        fun addPaths(parentPath: List) {
            val currentPath: MutableList = ArrayList(parentPath)
            currentPath.add(name)
            foldersWithRemovedNames!!.add(currentPath)
            for ((_, folder) in subFolders) {
                folder.addPaths(currentPath)
            }
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy