2000. Reverse Prefix of Word
Level: Easy
https://leetcode.com/problems/reverse-prefix-of-word/
class Solution:
def reversePrefix(self, word: str, ch: str) -> str:
i = word.find(ch)
if i == -1:
return word
return word[:i + 1][::-1] + word[i + 1:]
String item assignment is not allowed in Python (e.g. a[0] = 'b'
), so I used string concatenation instead.