Leetcode Problem 2000. Reverse Prefix of Word

2000. Reverse Prefix of Word

Leetcode Solutions

Two Pointers Approach

  1. Find the index of the first occurrence of ch in word. If ch is not found, return word as is.
  2. Initialize two pointers: left at the start of the string (index 0) and right at the index of ch.
  3. While left is less than right, swap the characters at left and right.
  4. Increment left and decrement right after each swap.
  5. Continue this process until left is greater than or equal to right.
  6. Return the modified string.
UML Thumbnail

Substring and Reverse

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...