-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReversePrefixOfWord.java
More file actions
39 lines (36 loc) · 1.12 KB
/
ReversePrefixOfWord.java
File metadata and controls
39 lines (36 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package leetcode;
/**
* ReversePrefixOfWord
* https://leetcode-cn.com/problems/reverse-prefix-of-word/
* 2000. 反转单词前缀
* https://leetcode-cn.com/problems/reverse-prefix-of-word/solution/bian-li-mo-ni-cao-zuo-by-oshdyr-tx9n/
*
* @author tobin
* @since 2022-02-02
*/
public class ReversePrefixOfWord {
public static void main(String[] args) {
ReversePrefixOfWord sol = new ReversePrefixOfWord();
System.out.println(sol.reversePrefix("abcdefd", 'd'));
System.out.println(sol.reversePrefix("xyxzxe", 'z'));
System.out.println(sol.reversePrefix("abcd", 'z'));
}
public String reversePrefix(String word, char ch) {
StringBuilder sb = new StringBuilder();
int loc = -1;
for (int i = 0; i < word.length(); i++) {
char curr = word.charAt(i);
sb.append(curr);
if (curr == ch) {
loc = i;
break;
}
}
if (loc < 0) {
return word;
}
StringBuilder nsb = sb.reverse();
nsb.append(word.substring(loc+1));
return nsb.toString();
}
}