URL: https://leetcode.com/problems/remove-all-occurrences-of-a-substring/ Signed-off-by: Matej Focko <me@mfocko.xyz>
14 lines
341 B
Kotlin
14 lines
341 B
Kotlin
class Solution {
|
|
fun removeOccurrences(
|
|
s: String,
|
|
part: String,
|
|
): String =
|
|
s.fold(StringBuilder(s.length)) { res, c ->
|
|
res.append(c)
|
|
if (res.endsWith(part)) {
|
|
res.deleteRange(res.length - part.length, res.length)
|
|
}
|
|
|
|
res
|
|
}.toString()
|
|
}
|