Reverse Words in a Sentence
Given a sentence (a string of words separated by single spaces), reverse the order of characters within each word, while maintaining the original word order and spaces between them.For example, if the input is "Hello World", the output should be "olleH dlroW".
[ "Hello World" ]
Explanation. The word "Hello" is reversed to "olleH" and "World" is reversed to "dlroW". The original word order and the space are maintained.
[ "Coding Challenge" ]
Explanation. Each word in the sentence is reversed independently.
[ "a b c" ]
Explanation. Single-letter words remain unchanged after reversal.
[ "racecar" ]
Explanation. A palindrome word is unchanged after reversal.
[ "The quick brown fox jumps over the lazy dog" ]
Explanation. A longer sentence demonstrates the reversal of each individual word.
Follow-up: Could you solve this problem in-place if the input were a mutable character array, without creating new string objects for words and with O(1) extra space?
The input string `s` will not have leading or trailing spaces.Words will be separated by a single space.Each word will consist of only English letters (uppercase or lowercase).`1 <= s.length <= 5000`
- Views
- 2