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

g0701_0800.s0726_number_of_atoms.Solution.kt Maven / Gradle / Ivy

There is a newer version: 1.30
Show newest version
package g0701_0800.s0726_number_of_atoms

// #Hard #String #Hash_Table #Sorting #Stack
// #2023_03_01_Time_149_ms_(100.00%)_Space_35.5_MB_(100.00%)

class Solution {
    data class Item(
        var title: String = "",
        var total: String = "",
        var count: Int = 0
    ) {
        fun getT(): String {
            return if (total == "") "1" else total
        }
    }

    fun countOfAtoms(formula: String): String {
        val set = arrayListOf()
        var current = Item()
        var open = 0
        var multiply = false
        var multiplier = ""
        fun multiplySet() {
            if (multiplier == "") {
                return
            }
            set.forEach {
                if (it.count > open) {
                    it.total = (it.getT().toInt() * multiplier.toInt()).toString()
                    it.count -= 1
                }
            }
            multiply = false
            multiplier = ""
        }
        formula.forEach {
            if (it !in '0'..'9') {
                multiplySet()
            }
            when (it) {
                in 'A'..'Z' -> {
                    current = Item()
                    set.add(current)
                    current.title = it.toString()
                    current.count = open
                    multiply = false
                }
                in 'a'..'z' -> {
                    current.title += it.toString()
                }
                '(' -> {
                    open++
                }
                ')' -> {
                    multiply = true
                    open--
                }
                in '0'..'9' -> {
                    if (multiply) {
                        multiplier += it.toString()
                    } else {
                        current.total += it.toString()
                    }
                }
            }
        }
        if (multiply) {
            multiplySet()
        }
        var result = ""
        set.groupBy { it.title }.toSortedMap().forEach {
            var count = 0
            it.value.forEach { v ->
                count += v.getT().toInt()
            }
            val c = if (count <= 1) "" else count.toString()
            result += "${it.key}$c"
        }
        return result
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy