Skip to content

Commit aba201b

Browse files
committed
reverse-words-in-a-string-iii
1 parent 9209c8e commit aba201b

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

easy/0557 reverse-words-in-a-string-iii/readme.md

Whitespace-only changes.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {string} s
3+
* @return {string}
4+
*/
5+
var reverseWords = function(s) {
6+
let outputString = '';
7+
for (let i = s.length - 1; i >= 0; i--) {
8+
if (s[i] === ' ' || i === 0) {
9+
let j = i === 0 ? i : i + 1;
10+
let word = '';
11+
while (s[j] !== ' ' && s[j] !== undefined) {
12+
word += s[j];
13+
j++;
14+
}
15+
outputString = (word.split('').reverse().join('') + ((i === s.length-1) ? "" : " "))+outputString;
16+
}
17+
}
18+
return outputString;
19+
};
20+
21+
console.log(reverseWords("Let's take LeetCode contest"));

0 commit comments

Comments
 (0)