187. Reverse Prefix of Word

EasyStringString

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 exist in word, return word unchanged.

Input: Two comma-separated values: string word and character ch.

Output: The modified string after reversing the prefix up to (and including) the first ch.

Examples

Example 1
Input: abcdefd,d
Output: dcbaefd
Explanation: First 'd' at index 3; reverse word[0:4]='abcd' → 'dcba'; append 'efd' → 'dcbaefd'.
Example 2
Input: xyxzxe,z
Output: zxyxxe
Explanation: First 'z' at index 3; reverse word[0:4]='xyxz' → 'zxyx'; append 'xe' → 'zxyxxe'.
Example 3
Input: abcd,z
Output: abcd
Explanation: 'z' not found — return unchanged.

Constraints

Asked by

BloombergAmazonGoogleMicrosoft
Solve this problem in the editor →