Kotlin Leetcode - 1662. Check If Two String Arrays are Equivalent
class Solution {
fun arrayStringsAreEqual(word1: Array<String>, word2: Array<String>): Boolean {
}
}
解題思路
這一題考的是對字串陣列的處理
透過 reduce
將陣列歸約成一個單一字串
或者使用 joinToString
可以很快的解決這個問題
Kotlin 參考解答
class Solution {
fun arrayStringsAreEqual(word1: Array<String>, word2: Array<String>): Boolean =
word1.reduce {acc, e -> acc + e} == word2.reduce {acc, e -> acc + e}
}
使用 joinToString
class Solution {
fun arrayStringsAreEqual(word1: Array<String>, word2: Array<String>): Boolean =
word1.joinToString("") == word2.joinToString("")
}
回到 leetcode 列表