g1001_1100.s1078_occurrences_after_bigram.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.s1078_occurrences_after_bigram
// #Easy #String #2023_06_01_Time_151_ms_(88.24%)_Space_36.5_MB_(70.59%)
class Solution {
fun findOcurrences(text: String, first: String, second: String): Array {
val list: MutableList = ArrayList()
val str = text.split(" ".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
for (i in str.indices) {
if (str[i] == first && str.size - 1 >= i + 2 && str[i + 1] == second) {
list.add(str[i + 2])
}
}
val s = arrayOfNulls(list.size)
var j = 0
for (ele in list) {
s[j++] = ele
}
return s
}
}