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

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

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

// #Easy #Array #String #Hash_Table #2023_03_06_Time_172_ms_(100.00%)_Space_36.3_MB_(70.00%)

@Suppress("NAME_SHADOWING")
class Solution {
    fun shortestCompletingWord(licensePlate: String, words: Array): String {
        var licensePlate = licensePlate
        licensePlate = licensePlate.lowercase()
        val a = IntArray(26)
        for (i in licensePlate.indices) {
            if (Character.isLetter(licensePlate[i])) {
                a[licensePlate[i].code - 'a'.code]++
            }
        }
        var ans = ""
        for (str in words) {
            val a1 = IntArray(26)
            for (element in str) {
                a1[element.code - 'a'.code]++
            }
            var j = 0
            while (j < 26) {
                if (a[j] <= a1[j]) {
                    j++
                } else {
                    break
                }
            }
            if (j == 26 && (ans.isEmpty() || ans.length > str.length)) {
                ans = str
            }
        }
        return ans
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy