[LeetCode] 2000. Reverse Prefix of Word

Given a 0-indexed string word and a character ch, reverse the segment of word that starts at index 0 and ends at the index of the first occurrence of ch (inclusive). If the character ch does not exist in word, do nothing.

For example, if word = “abcdefd” and ch = “d”, then you should reverse the segment that starts at 0 and ends at 3 (inclusive). The resulting string will be “dcbaefd”.
Return the resulting string.

Example 1:
Input: word = “abcdefd”, ch = “d”
Output: “dcbaefd”
Explanation: The first occurrence of “d” is at index 3.
Reverse the part of word from 0 to 3 (inclusive), the resulting string is “dcbaefd”.

Example 2:
Input: word = “xyxzxe”, ch = “z”
Output: “zxyxxe”
Explanation: The first and only occurrence of “z” is at index 3.
Reverse the part of word from 0 to 3 (inclusive), the resulting string is “zxyxxe”.

Example 3:
Input: word = “abcd”, ch = “z”
Output: “abcd”
Explanation: “z” does not exist in word.
You should not do any reverse operation, the resulting string is “abcd”.

Constraints:
1 <= word.length <= 250
word consists of lowercase English letters.
ch is a lowercase English letter.

反转单词前缀。

给你一个下标从 0 开始的字符串 word 和一个字符 ch 。找出 ch 第一次出现的下标 i ,反转 word 中从下标 0 开始、直到下标 i 结束(含下标 i )的那段字符。如果 word 中不存在字符 ch ,则无需进行任何操作。

例如,如果 word = “abcdefd” 且 ch = “d” ,那么你应该 反转 从下标 0 开始、直到下标 3 结束(含下标 3 )。结果字符串将会是 “dcbaefd” 。
返回 结果字符串 。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/reverse-prefix-of-word
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路

题意不难理解,反转 input 字符串的某一个前缀,返回反转之后的结果。需要考虑的 corner case 是如果字符串中不存在目标字母,则返回原字符串。一般的 case 是如果找到了目标字母第一次出现的位置 i,则对这个前缀 (0, i) 进行反转,与字符串剩余的部分拼接好之后返回即可。

复杂度

时间O(n)
空间O(n) - StringBuilder

代码

Java实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public String reversePrefix(String word, char ch) {
int i = word.indexOf(ch);
String a = reverse(word, 0, i);
return a;
}

private String reverse(String word, int start, int end) {
char[] letters = word.toCharArray();
while (start < end) {
char temp = letters[start];
letters[start] = letters[end];
letters[end] = temp;
start++;
end--;
}
return String.valueOf(letters);
}
}

再提供一个第一次写的像X一样的代码
Java实现

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
40
41
42
class Solution {
public String reversePrefix(String word, char ch) {
// corner case
if (!word.contains(ch + "")) {
return word;
}

// normal case
StringBuilder sb = new StringBuilder();
int i = 0;
while (i < word.length()) {
if (word.charAt(i) == ch) {
sb.append(helper(word, 0, i));
break;
}
i++;
}
i++;

while (i < word.length()) {
sb.append(word.charAt(i));
i++;
}
return sb.toString();
}

private String helper(String word, int start, int end) {
char[] w = word.substring(start, end + 1).toCharArray();
while (start < end) {
char temp = w[start];
w[start] = w[end];
w[end] = temp;
start++;
end--;
}
StringBuilder sb = new StringBuilder();
for (char c : w) {
sb.append(c);
}
return sb.toString();
}
}

[LeetCode] 2000. Reverse Prefix of Word
https://shurui91.github.io/posts/2794589433.html
Author
Aaron Liu
Posted on
February 6, 2022
Licensed under