g1001_1100.s1002_find_common_characters.Solution.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-kotlin Show documentation
Show all versions of leetcode-in-kotlin Show documentation
Kotlin-based LeetCode algorithm problem solutions, regularly updated
package g1001_1100.s1002_find_common_characters
// #Easy #Array #String #Hash_Table #2023_05_14_Time_181_ms_(100.00%)_Space_40.6_MB_(37.50%)
class Solution {
fun commonChars(words: Array?): List {
if (words == null) {
throw RuntimeException("words null")
}
if (words.isEmpty()) {
return ArrayList()
}
var tmp = words[0]
for (i in 1 until words.size) {
tmp = getCommon(tmp, words[i])
}
val result: MutableList = ArrayList()
for (element in tmp) {
result.add(element.toString())
}
return result
}
private fun getCommon(s1: String, s2: String): String {
if (s1.isEmpty() || s2.isEmpty()) {
return ""
}
val c1c = countChars(s1)
val c2c = countChars(s2)
val sb = StringBuilder()
for (i in c1c.indices) {
var m = c1c[i].coerceAtMost(c2c[i])
while (m > 0) {
sb.append(('a'.code + i).toChar())
m--
}
}
return sb.toString()
}
private fun countChars(str: String): IntArray {
val result = IntArray(26)
for (element in str) {
result[element.code - 'a'.code]++
}
return result
}
}