0

Working on this reverse words in string

I tried to revert part of the string, given the input string, start_index, end_index, but it seems no effect.

var myrev = function(str, s, e) {
    let tmp;
    while(s<e) {
        tmp = str[s];        
        str[s] = str[e];        
        str[e] = tmp;

        s++;
        e--;
    }

    return str;
}

full code

// revert the words in this string: " the  sky is   blue "
var reverseWords = function(s) {
    let a = s.trim().replace(/\s+/g, ' ');
    a = a.split('');

    // s === "eulb si yks eht"
    s = a.reverse().join('');

    let i, j, ind;
    ind = s.indexOf(' ', 0);
    i=0;
    j=ind-1;

    // expect s === "blue si yks eht", but still "eulb si yks eht", why???????
    s = myrev(s, i, j);

    console.log(s)
};


var myrev = function(str, s, e) {
    let tmp;
    while(s<e) {
        tmp = str[s];        
        str[s] = str[e];        
        str[e] = tmp;

        s++;
        e--;
    }

    return str;
}

Does it seem I need to pass a string as a reference?

Lin Du
  • 88,126
  • 95
  • 281
  • 483
kenpeter
  • 7,404
  • 14
  • 64
  • 95

2 Answers2

0

You need to split() by a \s+(one or more space). And join() by a ' '(whitespace) not empty string ''. There is no need of myrev for leetcode problem. The below code is passing all tests for leetcode.

var reverseWords = function(s) {
    return s.trim().split(/\s+/).reverse().join(' ')
};

The notation str[index] in only to access the letter of string at that index. Changing it will not change the string. You can first convert string to array of letters and then use join() at end.

var myrev = function(str, s, e) {
    str = [...str]
    let tmp;
    while(s<e) {
        tmp = str[s];        
        str[s] = str[e];        
        str[e] = tmp;
        s++;
        e--;
    }
    return str.join('');
}

console.log(myrev("eulb si yks eht",0,3))
Maheer Ali
  • 35,834
  • 5
  • 42
  • 73
  • Yes, this is the solution, but is it a way to modify the string in place? – kenpeter Apr 27 '19 at 13:10
  • @kenpeter, did you read the first comment under your question? ... "immutable"... – trincot Apr 27 '19 at 13:11
  • @kenpeter I couldnot understand what you mean. If `str = 'abc'`. `str[1] = str[2]` will not change the real string. You can use `slice()` for that. – Maheer Ali Apr 27 '19 at 13:12
  • @kenpeter here u can find a good explanation of the differences https://stackoverflow.com/questions/373419/whats-the-difference-between-passing-by-reference-vs-passing-by-value – Thecave3 Apr 27 '19 at 13:16
  • @MaheerAli, myrev(str, s, e), str: input string, s: start_index, e: end_index. e.g. "eulb si yks eht" ==> "blue si yks eht", if start_index = 0, end_index = 3 – kenpeter Apr 27 '19 at 13:18
0

string can’t be modified

var myrev = function(str, s, e) {
let tmpArr = str.split("")
while(s<e) {
    tmp = tmpArr[s];        
    tmpArr[s] = tmpArr[e];        
    tmpArr[e] = tmp;
    s++;
    e--;
}
return tmpArr.join("");

}

fbfatboy
  • 371
  • 1
  • 6