Kotlin Leetcode - 1221. Split a String in Balanced Strings
class Solution {
fun balancedStringSplit(s: String): Int {
}
}
解題思路
這一題考的是對字串的處理
可以用很直觀的邏輯判斷輸入
Kotlin 參考解答
class Solution {
fun balancedStringSplit(s: String): Int {
var split = 0
var rc = 0
var lc = 0
for (c in s) {
if (c == 'R') rc++
if (c == 'L') lc++
if (lc == rc) {
rc = 0
lc = 0
split++
}
}
return split
}
}
回到 leetcode 列表