Leetcode Offer 58 - Rotate string leftly

Note:

  • Normal approach is easy to come up with, but try to using O(1) extra space.
  • Original length is length = s.length
  • Along with concatenating the last char to s, we find that we are always using newS[oriLength - 1].

Question:

Reverse string from index k to the left.

Example:

1
2
Input: s = "abcdefg", k = 2
Output: "cdefgab

Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
* @param {string} s
* @param {number} n
* @return {string}
*/
var reverseLeftWords = function(s, n) {
const length = s.length;
let i = 0;
while (i < length - n) {
s = s[length - 1] + s;
i++;
}
return s.slice(0, length);
};