Reverse Prefix of Word

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 ch does not occur in word, return word unchanged. Return the resulting string.

30:00

Reverse Prefix of Word
easy
Free written solution
Topics
Companies

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 ch does not occur in word, return word unchanged. Return the resulting string.

Example 1:
Input: {"word":"abcdefd","ch":"d"}
Output: dcbaefd
Constraints:
  • 1≤word.length≤2501 \leq \text{word.length} \leq 250

  • word consists of lowercase English letters.

  • ch is a lowercase English letter.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"word":"abcdefd","ch":"d"}

Look for the first 'd' in "abcdefd". Everything up to and including it is the prefix to reverse.

word, looking for ch = 'd'

a

0

b

1

c

2

d

3

e

4

f

5

d

6

Variables
VariableValue
chd
len(word)7
DepthFunction Call
Stack empty
0/14