kt: add «1910. Remove All Occurrences of a Substring»

URL:	https://leetcode.com/problems/remove-all-occurrences-of-a-substring/
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2025-02-11 14:33:15 +01:00
parent 3425681c53
commit fa27bdd99c
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

View file

@ -0,0 +1,14 @@
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()
}