Skip to content

Practice Reverse Prefix of Word

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Easy#2000
LeetCode Problem

Reverse Prefix of Word

Problem Statement

Given a 0-indexed string word and a character ch, reverse the segment of word that starts at index 0 and ends at the index of the first occurrence of ch (inclusive). If the character ch does not exist in word, do nothing. Return the resulting string.

Source: Reverse Prefix of Word on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
word = "abcdefd", ch = "d"
Output
"dcbaefd"

# Example 2

Input
word = "xyxzxe", ch = "z"
Output
"zxyxxe"

# Example 3

Input
word = "abcd", ch = "z"
Output
"abcd"

Constraints

1 <= word.length <= 250
word consists of lowercase English letters.
ch is a lowercase English letter.

Related LeetCode Problems

Practice more problems similar to Reverse Prefix of Word.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.