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:
parent
3425681c53
commit
fa27bdd99c
1 changed files with 14 additions and 0 deletions
14
kt/remove-all-occurrences-of-a-substring.kt
Normal file
14
kt/remove-all-occurrences-of-a-substring.kt
Normal 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()
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue