Given a string s
, reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.
Example 1:
Input: s = "Let's take LeetCode contest"
Output: "s'teL ekat edoCteeL tsetnoc"
Example 2:
Input: s = "Mr Ding"
Output: "rM gniD"
Constraints:
1 <= s.length <= 5 * 10^4
s
contains printable ASCII characters.s
does not contain any leading or trailing spaces.s
.s
are separated by a single space.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 approach for reversing words in a string individually involves splitting the main string into separate words first. Then, for each word, we completely reverse the order of its letters before putting the reversed words back together into a new string.
Here's how the algorithm would work step-by-step:
def reverse_words_in_string(input_string):
words = input_string.split()
reversed_words = []
# Iterate through each word in the list of words
for word in words:
reversed_word = ''
# Reverse the characters in the current word
for i in range(len(word) - 1, -1, -1):
reversed_word += word[i]
reversed_words.append(reversed_word)
# Join the reversed words back into a string
return ' '.join(reversed_words)
The goal is to reverse each word in a given sentence, while keeping the original order of the words. The efficient approach is to split the sentence into individual words, reverse each word separately, and then combine them back together. This avoids unnecessary operations and directly addresses the problem's core requirement.
Here's how the algorithm would work step-by-step:
def reverse_words_in_string(input_string):
words = input_string.split()
reversed_words = []
for word in words:
# Reverse each word to meet problem requirements.
reversed_word = word[::-1]
reversed_words.append(reversed_word)
# Rejoin the reversed words into a single string
return ' '.join(reversed_words)
Case | How to Handle |
---|---|
Null or empty input string | Return an empty string or null depending on the requirements since there are no words to reverse. |
String with leading/trailing spaces | Trim leading and trailing spaces before processing to avoid incorrect word boundaries. |
String with multiple spaces between words | Normalize spaces to single spaces between words before reversing to ensure correct word separation. |
String containing only spaces | Return an empty string after trimming since there are no actual words to reverse. |
String with a single word | Return the single word itself since reversing its words has no effect. |
String with special characters or non-alphanumeric characters | Handle these characters as part of the word to reverse, unless specifically excluded by the problem. |
Maximum string length (memory constraints) | Consider using an in-place reversal algorithm if memory usage is a critical concern. |
String with very long words | Ensure the reversal algorithm handles very long words efficiently without causing performance issues. |