mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-09 15:59:06 +01:00
java: add «2000. Reverse Prefix of Word»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
4e873f87b0
commit
8c3d5463ea
1 changed files with 18 additions and 0 deletions
18
java/reverse-prefix-of-word.java
Normal file
18
java/reverse-prefix-of-word.java
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue