Reverse Through the First Matching Character. Find the first occurrence of ch in word. Reverse the prefix starting at zero and ending at that occurrence, inclusive, leaving the suffix unchanged. If ch never appears, return word unchanged.
### Problem
Reverse the prefix of `word` from index 0 through the first occurrence of `ch`, including that occurrence. Leave every later character unchanged. If `ch` is absent, return the original string.
```cpp
class Solution {
public:
string reversePrefix(string word, char ch);
};
```
### Examples
**Example 1:**
```text
word = "planet", ch = 'n'
Output: "nalpet"
Explanation: The prefix "plan" becomes "nalp".
```
**Example 2:**
```text
word = "level", ch = 'x'
Output: "level"
Explanation: The target character is absent.
```
### Constraints
- `1 <= word.length <= 250`.
- word and ch use lowercase English letters.
### Clarifying Questions
Use the first matching position, even if ch appears later as well.
```hint Inspect the structure
What iterator endpoint includes the found character in a reversal?
```
### Reasoning checkpoints
- Reverse exactly the required inclusive prefix and preserve the remaining suffix.
- Handle an absent target and a first match at either endpoint of the string.
### Optional extensions
How could you perform the required reversal without calling a library reversal function?
Overview: Reverse Through the First Matching Character. Find the first occurrence of ch in word. Reverse the prefix starting at zero and ending at that occurrence, inclusive, leaving the suffix unchanged. If ch never appears, return word unchanged.
Reverse the prefix of word from index 0 through the first occurrence of ch, including that occurrence. Leave every later character unchanged. If ch is absent, return the original string.
class Solution {
public:
string reversePrefix(string word, char ch);
};
Examples
Example 1:
word = "planet", ch = 'n'
Output: "nalpet"
Explanation: The prefix "plan" becomes "nalp".
Example 2:
word = "level", ch = 'x'
Output: "level"
Explanation: The target character is absent.
Constraints
1 <= word.length <= 250
.
word and ch use lowercase English letters.
Clarifying Questions Guidance
Use the first matching position, even if ch appears later as well.
Reasoning checkpoints
Reverse exactly the required inclusive prefix and preserve the remaining suffix.
Handle an absent target and a first match at either endpoint of the string.
Optional extensions
How could you perform the required reversal without calling a library reversal function?