package daily

fun main() {
    // 811. 子域名访问计数
    // https://leetcode.cn/problems/subdomain-visit-count/
    println(subdomainVisits(arrayOf("900 google.mail.com", "50 yahoo.com", "1 intel.mail.com", "5 wiki.org")))
}

fun subdomainVisits(cpdomains: Array<String>): List<String> {

    val map = HashMap<String ,Int>()

    for (cp in cpdomains) {
        val split = cp.split(" ")
        val count = split[0].toInt()
        val domain = split[1]
        val subDomains = domain.split(".")
        // 域名最后一段计数
        val dLast = subDomains[subDomains.size - 1]
        var lastCount = map[dLast]
        if (lastCount == null) {
            lastCount = count
        }else {
            lastCount += count
        }
        map[dLast] = lastCount

        // 域名中间段计数
        val dMid = subDomains[subDomains.size - 2] + "." + dLast
        var midCount = map[dMid]
        if (midCount == null) {
            midCount = count
        }else {
            midCount += count
        }
        map[dMid] = midCount

        // 域名第一段计数，可能没有
        if (subDomains.size > 2) {
            val dFirst = subDomains[subDomains.size - 3] + "." + dMid
            var firstCount = map[dFirst]
            if (firstCount == null) {
                firstCount = count
            }else {
                firstCount += count
            }
            map[dFirst] = firstCount
        }
    }

    val result = ArrayList<String>()
    map.forEach {
        result.add("${it.value} ${it.key}")
    }

    return result
}
package daily

fun main() {
    // 811. 子域名访问计数
    // https://leetcode.cn/problems/subdomain-visit-count/
    println(subdomainVisits(arrayOf("900 google.mail.com", "50 yahoo.com", "1 intel.mail.com", "5 wiki.org")))
}

fun subdomainVisits(cpdomains: Array<String>): List<String> {

    val map = HashMap<String ,Int>()

    for (cp in cpdomains) {
        val split = cp.split(" ")
        val count = split[0].toInt()
        val domain = split[1]
        val subDomains = domain.split(".")
        // 域名最后一段计数
        val dLast = subDomains[subDomains.size - 1]
        var lastCount = map[dLast]
        if (lastCount == null) {
            lastCount = count
        }else {
            lastCount += count
        }
        map[dLast] = lastCount

        // 域名中间段计数
        val dMid = subDomains[subDomains.size - 2] + "." + dLast
        var midCount = map[dMid]
        if (midCount == null) {
            midCount = count
        }else {
            midCount += count
        }
        map[dMid] = midCount

        // 域名第一段计数，可能没有
        if (subDomains.size > 2) {
            val dFirst = subDomains[subDomains.size - 3] + "." + dMid
            var firstCount = map[dFirst]
            if (firstCount == null) {
                firstCount = count
            }else {
                firstCount += count
            }
            map[dFirst] = firstCount
        }
    }

    val result = ArrayList<String>()
    map.forEach {
        result.add("${it.value} ${it.key}")
    }

    return result
}