LeetCode solutions

2128. Reverse Prefix of Word

My accepted Python solution to LeetCode problem 2128, Reverse Prefix of Word, running in 0ms.

  • Difficulty: Easy
  • Python
  • Runtime 0ms
  • Memory 17.5MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 0ms, memory 17.5MB, accepted 2025-12-29.

python
class Solution:
    def reversePrefix(self, word: str, ch: str) -> str:
        idx = word.find(ch)
        if idx == -1:
            return word
        return word[:idx+1][::-1] + word[idx+1:]

Source