2000. 反转单词前缀
Ideas
Python可以直接通过index定位ch的索引,然后前半部分通过切片和[::-1]进行翻转,后半部分通过切片直接拼接。
Code
Python
class Solution:
def reversePrefix(self, word: str, ch: str) -> str:
return word if ch not in word else word[:word.index(ch) + 1][::-1] + word[word.index(ch) + 1:]