1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-11-09 15:59:06 +01:00
LeetCode/java/reverse-prefix-of-word.java
Matej Focko 8c3d5463ea
java: add «2000. Reverse Prefix of Word»
Signed-off-by: Matej Focko <me@mfocko.xyz>
2024-05-01 21:25:05 +02:00

18 lines
367 B
Java

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();
}
}