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

g0801_0900.s0811_subdomain_visit_count.Solution.kt Maven / Gradle / Ivy

There is a newer version: 1.32
Show newest version
package g0801_0900.s0811_subdomain_visit_count

// #Medium #Array #String #Hash_Table #Counting
// #2023_03_17_Time_220_ms_(100.00%)_Space_36.6_MB_(100.00%)

class Solution {
    fun subdomainVisits(d: Array): List {
        val fmap: MutableMap = HashMap()
        for (s in d) {
            var rep = 0
            var i: Int = 0
            while (i < s.length) {
                val c = s[i]
                rep = if (c in '0'..'9') {
                    rep * 10 + (c.code - '0'.code)
                } else {
                    break
                }
                i++
            }
            val str = s.substring(i + 1)
            seperate(rep, str, fmap)
        }
        val res: MutableList = ArrayList()
        for (entry in fmap.entries.iterator()) {
            var comp = ""
            comp += entry.value
            comp += " "
            comp += entry.key
            res.add(comp)
        }
        return res
    }

    private fun seperate(rep: Int, s: String, fmap: MutableMap) {
        var i = s.length - 1
        while (i >= 0) {
            while (i >= 0 && s[i] != '.') {
                i--
            }
            val toHash: String = s.substring(i + 1)
            fmap[toHash] = fmap.getOrDefault(toHash, 0) + rep
            i--
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy