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

g0601_0700.s0609_find_duplicate_file_in_system.Solution.kt Maven / Gradle / Ivy

There is a newer version: 1.30
Show newest version
package g0601_0700.s0609_find_duplicate_file_in_system

// #Medium #Array #String #Hash_Table #2023_02_03_Time_426_ms_(100.00%)_Space_48.5_MB_(80.00%)

class Solution {
    fun findDuplicate(paths: Array): List> {
        val map: MutableMap> = HashMap()
        for (path in paths) {
            val pathComponents = path.split(" ".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
            val root = pathComponents[0]
            for (i in 1 until pathComponents.size) {
                val startIndex = pathComponents[i].indexOf("(")
                val endIndex = pathComponents[i].lastIndexOf(")")
                val content = pathComponents[i].substring(startIndex, endIndex)
                map.putIfAbsent(content, ArrayList())
                map[content]!!.add(root + "/" + pathComponents[i].substring(0, startIndex))
            }
        }
        val result: MutableList> = ArrayList()
        for (list in map.values) {
            if (list.size > 1) {
                result.add(list)
            }
        }
        return result
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy