题目链接:2000. 反转单词前缀
找到位置,反转即可。
class Solution:
def reversePrefix(self, word: str, ch: str) -> str:
try:
idx = word.index(ch)
return word[0:idx + 1][::-1] + word[idx + 1:]
except ValueError:
return word