Reverse Words (Google Code Jam)246 views
function reverseWords(str){
/**
* sub routine to reverse a string
* @param {Array} ar array of characters
* @param {number} st start index
* @param {number} end end index
* @return {Array} array elements in reverse order
*/
function reverseStr(ar,st,end){
var i,tmp;
var mid = Math.floor((st+end)/2);
for(i=st;i<=mid;i++){
tmp = ar[i];
ar[i]=ar[end-i+st];
ar[end-i+st] = tmp;
}
return ar;
}
// convert string to mutable array of chars
var rv = [],tmp;
for(var i=0;i<str.length;i++){
rv.push(str.charAt(i));
}
// reverse every word in string
var start = 0;
for(i=0;i<rv.length;i++){
if(rv[i]==' '){
reverseStr(rv,start,i-1);
start=i+1;
}
}
// reverse the last word
reverseStr(rv,start,rv.length-1);
//reverse the whole string
rv = reverseStr(rv,0,rv.length-1);
// return after converting array to string again
return rv.join('');
}
Problem Statement: Given a string containing words separated by single space, return a string that contains these words in reverse order
Solution: Solution is to reverse the individual words first and then to reverse the entire string. For example, if input string is “one algorithm everyday”
then first reverse every word so that we have “eno mhtirogla yadyreve”.
now we need to reverse the entire string so that we have “everyday algorithm one”, which is the desired result.