-
Notifications
You must be signed in to change notification settings - Fork 40
/
ReverseWordsInAString.js
35 lines (32 loc) · 1.01 KB
/
ReverseWordsInAString.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Source : https://oj.leetcode.com/problems/reverse-words-in-a-string/
// Author : Dean Shi
// Date : 2015-06-13
/**********************************************************************************
*
* Given an input string, reverse the string word by word.
*
* For example,
* Given s = "the sky is blue",
* return "blue is sky the".
*
*
* Clarification:
*
* What constitutes a word?
* A sequence of non-space characters constitutes a word.
* Could the input string contain leading or trailing spaces?
* Yes. However, your reversed string should not contain leading or trailing spaces.
* How about multiple spaces between two words?
* Reduce them to a single space in the reversed string.
*
*
**********************************************************************************/
/**
* @param {string} str
* @returns {string}
*/
var reverseWords = function(str) {
return str.trim().split(/ +/).reverse().join(' ')
};
// Test case
console.log(reverseWords('the sky is blue') === 'blue is sky the'); // true