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.
word = "abcdefd" and ch = "d", then you should reverse the segment that starts at 0 and ends at 3 (inclusive). The resulting string will be "dcbaefd".Return the resulting string.
Example 1:
Input: word = "abcdefd", ch = "d" Output: "dcbaefd" Explanation: The first occurrence of "d" is at index 3. Reverse the part of word from 0 to 3 (inclusive), the resulting string is "dcbaefd".
Example 2:
Input: word = "xyxzxe", ch = "z" Output: "zxyxxe" Explanation: The first and only occurrence of "z" is at index 3. Reverse the part of word from 0 to 3 (inclusive), the resulting string is "zxyxxe".
Example 3:
Input: word = "abcd", ch = "z" Output: "abcd" Explanation: "z" does not exist in word. You should not do any reverse operation, the resulting string is "abcd".
Constraints:
1 <= word.length <= 250word consists of lowercase English letters.ch is a lowercase English letter.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force strategy for reversing a word's prefix involves checking the word character by character to see if we find a specified target. If we find the target, we reverse everything up to and including that target. If we don't find it, we return the original word.
Here's how the algorithm would work step-by-step:
def reverse_prefix_brute_force(word, character):
for index in range(len(word)):
# Check if the current character matches the target.
if word[index] == character:
# Reverse the prefix up to and including the target.
prefix_to_reverse = word[:index + 1]
reversed_prefix = prefix_to_reverse[::-1]
# Concatenate reversed prefix with remaining part of word.
remaining_word = word[index + 1:]
return reversed_prefix + remaining_word
# If the target is not found return the original word.
return wordThe most efficient way to solve this problem is to first locate the target character in the word. If found, we reverse the portion of the word up to and including that character; otherwise, we return the original word.
Here's how the algorithm would work step-by-step:
def reverse_prefix_of_word(word, character):
string_length = len(word)
character_index = -1
for index in range(string_length):
if word[index] == character:
character_index = index
break
# If the target isn't found, there's nothing to reverse.
if character_index == -1:
return word
# Extract the prefix up to and including the target character.
prefix_to_reverse = word[:character_index + 1]
# Reverse the prefix string.
reversed_prefix = prefix_to_reverse[::-1]
# Combine the reversed prefix with the rest of the string.
remaining_string = word[character_index + 1:]
result = reversed_prefix + remaining_string
return result| Case | How to Handle |
|---|---|
| word is null or empty | Return the empty string directly, as there's nothing to reverse. |
| ch is not found in word | Return the original word unchanged, as no reversal is needed. |
| word contains only one character | If the single character matches ch, reverse the prefix which is just the same single character. |
| word contains only ch | Reverse the entire word (which is just the same character repeated) up to and including the last occurrence of ch. |
| ch is the first character of word | Reverse the first character (trivially the same character). |
| word is very long (close to maximum string length) | Ensure the algorithm does not exceed memory limitations by reversing the prefix in-place efficiently using StringBuilder or character array manipulation. |
| Multiple occurrences of ch exist in word | The algorithm should only reverse the prefix up to the first occurrence of ch. |
| ch is a special character (e.g., unicode, non-ASCII) | The character comparison should correctly handle unicode or non-ASCII characters. |