I’ve used Brute-force to solve this problem. Other people used Trie to solve but I’m not yet familiar with it.
class Solution {
fun indexPairs(text: String, words: Array<String>): Array<IntArray> {
var low = 0
var high = low + 1
var output = ArrayList<IntArray>()
while(low < text.length){
if(text.substring(low, high) in words){
output.add(intArrayOf(low, high-1))
}
high++
if(high > text.length) {
low++
high = low
}
}
return output.toTypedArray()
}
}
Runtime: 260 ms, faster than 33.33% of Kotlin online submissions for Index Pairs of a String.
Memory Usage: 37.6 MB, less than 100.00% of Kotlin online submissions for Index Pairs of a String.