java: add «2000. Reverse Prefix of Word»

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-05-01 21:25:05 +02:00
parent 4e873f87b0
commit 8c3d5463ea
Signed by: mfocko
GPG key ID: 7C47D46246790496

View file

@ -0,0 +1,18 @@
class Solution {
public String reversePrefix(String word, char ch) {
var sb = new StringBuilder(word.length());
var canSwap = true;
for (var i = 0; i < word.length(); ++i) {
char c = word.charAt(i);
sb.append(c);
if (c == ch && canSwap) {
sb.reverse();
canSwap = false;
}
}
return sb.toString();
}
}