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 * 104s 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 goal is to reverse each word within a sentence while keeping the word order intact. The brute force method involves examining each word one at a time and performing the reversal operation directly. Afterwards, the reversed words are put back together.
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
for word in words:
word_characters = list(word)
left_index = 0
right_index = len(word_characters) - 1
#Reverse the word using two pointers
while left_index < right_index:
word_characters[left_index], word_characters[right_index] = \
word_characters[right_index], word_characters[left_index]
left_index += 1
right_index -= 1
reversed_word = "".join(word_characters)
reversed_words.append(reversed_word)
#Join the reversed words with spaces
return " ".join(reversed_words)The main idea is to process each word in the given string individually. We'll go through each word, reverse it, and then put the reversed word back into a new string with spaces in the correct places.
Here's how the algorithm would work step-by-step:
def reverse_words_in_string(input_string):
list_of_words = input_string.split()
reversed_string = ""
for i in range(len(list_of_words)):
# Reverse each word individually.
reversed_word = list_of_words[i][::-1]
reversed_string += reversed_word
# Avoid adding space after the last word.
if i < len(list_of_words) - 1:
reversed_string += " "
return reversed_string
def main():
input_string = "Let's take LeetCode contest"
# Demonstrates the function with a sample input.
result = reverse_words_in_string(input_string)
print(result)
if __name__ == "__main__":
main()| Case | How to Handle |
|---|---|
| Null or Empty Input String | Return an empty string or throw an IllegalArgumentException as appropriate for the use case. |
| String with only whitespace | Should return the same string, preserving the whitespace only string without reversing anything. |
| String with leading and trailing whitespace | Whitespace should be preserved at the beginning and end; reverse each word individually, then stitch it back. |
| String with consecutive whitespace characters between words | The solution must handle consecutive whitespace correctly by preserving the multiple spaces and only reversing the words separated by them. |
| String containing only one word | Reverse the characters of the single word and return it, preserving any surrounding whitespace. |
| String with very long words (close to maximum string size) | Ensure that reversing very long words does not cause memory issues or stack overflow if recursion is used and test performance to see if improvements could be made. |
| String containing non-ASCII characters (Unicode) | Ensure that character reversal handles Unicode characters correctly, as some languages have multi-byte character encodings. |
| String with special characters (e.g., punctuation, symbols) | Treat special characters the same way as regular alphanumeric characters, reversing them within their words. |