We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent de2cef9 commit 9f07231Copy full SHA for 9f07231
2000.Reverse-Prefix-of-Word.java
@@ -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