Naive Approach To Reversing Words In A String
Naive Approach: Reverse Words in String Reverse words in a string without built-in functions, using loops and arrays. Time Complexity O(n), Space Complexity O(n).
Naive Approach /** * @param {string} s * @return {string} */ var reverseWords = function(s) { const arr = s.split(' '); const res = []; for(let i =0;i<arr.length;i++){ let lastword = arr[arr.length-i-1]; if(lastword != ''){ res.push(lastword) } } return res.join(' ') }; Time Complexity is O(n) as we are iterating through the words we splitted , (n is the words in string) Space Complexity is O(n) as we are using res variable array to store Using Regex /** * @param {string} s * @return {string} */ var...