Anjan Dutta

Leet Code 344: Reverse String

Leet Code 344: Reverse String

Problem statement:

Write a function that reverses a string. The input string is given as an array of characters s.

You must do this by modifying the input array in-place with O(1) extra memory.

Constraints:

  • 1 <= s.length <= 105
  • s[i] is a printable ascii character.

Soultion:

/**
* @param {character[]} s
* @return {void} Do not return anything, modify s in-place instead.
*/
var reverseString = function(s) {
let r = s.length -1;
let l = 0;
while (l<r) {
let temp = s[l];
s[l] = s[r];
s[r] = temp;
l++;
r--;
}
};