Skip to content

Commit 9f07231

Browse files
committed
feat(leetcode): add No.2000
1 parent de2cef9 commit 9f07231

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

2000.Reverse-Prefix-of-Word.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// https://leetcode.com/problems/reverse-prefix-of-word/
2+
// algorithms
3+
// Easy (80.9%)
4+
// Total Accepted: 7.2K
5+
// Total Submissions: 8.9K
6+
7+
8+
class Solution {
9+
public String reversePrefix(String word, char ch) {
10+
int idx = -1;
11+
int len = word.length();
12+
13+
for (int i = 0; i < len; i++) {
14+
if (word.charAt(i) == ch) {
15+
idx = i;
16+
break;
17+
}
18+
}
19+
20+
if (idx != -1) {
21+
StringBuilder sb = new StringBuilder();
22+
for (int i = idx; i >= 0; i--) {
23+
sb.append(word.charAt(i));
24+
}
25+
sb.append(word.substring(idx + 1));
26+
27+
return sb.toString();
28+
}
29+
30+
return word;
31+
}
32+
}

0 commit comments

Comments
 (0)