Skip to content

Latest commit

 

History

History
71 lines (49 loc) · 1.6 KB

JavaScriptReverseString.md

File metadata and controls

71 lines (49 loc) · 1.6 KB

JavaScript Reverse String


Challenge

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.

1st Example

Input: s = ['s','u','p','e','r','k','l','o','k']
Output: ['k','o','l','k','r','e','p','u','s']

2nd Example

Input: s = ['R','a','c','e','c','a','r']
Output: ['r','a','c','e','c','a','R']

Constraints

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

Solution

const reverseString = (s) => {
    s.reverse();
};

Explanation

I've created a function expression, using ES6 syntax, that is defined by an anonymous function that accepts s as an array of string characters.

Within the anonymous function, the reverse array prototype method is applied to the string array s. This will output the reverse of any array that the reverseString function is applied to.