[JS] Reverse Words in a String III
2022. 7. 29. 16:02
🔒 문제 (LeetCode 557)
Given a string s, reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.
Constraints:
- 1 <= s.length <= 5 * 104
- s contains printable ASCII characters.
- s does not contain any leading or trailing spaces.
- There is at least one word in s.
- All the words in s are separated by a single space.
🌊 입출력
Example 1:
Input: s = "Let's take LeetCode contest"
Output: "s'teL ekat edoCteeL tsetnoc"
Example 2:
Input: s = "God Ding"
Output: "doG gniD"
🔑 해결
🌌 알고리즘 - two pointers
/**
* @param {string} s
* @return {string}
*/
var reverseWords = function(s) {
return s.split(' ').map(w => w.split('').reverse().join('')).join(' ');
};
'코딩테스트 (JS) > 투포인터' 카테고리의 다른 글
[JS] Remove Nth Node From End of List (0) | 2022.07.29 |
---|---|
[JS] Middle of the Linked List (0) | 2022.07.29 |
[JS] Reverse String (0) | 2022.07.29 |
[JS] Two Sum II - Input Array Is Sorted (0) | 2022.07.29 |
[JS] Move Zeroes (0) | 2022.07.29 |