:orphan: Reverse String ============== .. highlight:: none Problem ------- https://leetcode.com/problems/reverse-string/ 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.   **Example 1:** :: Input: s = ["h","e","l","l","o"] Output: ["o","l","l","e","h"] **Example 2:** :: Input: s = ["H","a","n","n","a","h"] Output: ["h","a","n","n","a","H"]   **Constraints:** - ``1 <= s.length <= 10``\ :sup:```5``` - ``s[i]`` is a `printable ascii character `__. .. highlight:: python Pattern ------- Two Pointers, String Solution -------- Let :math:`n` be the length of the input list ``s``. Let ``end = n - 1``. We can reverse the list by swapping the outermost elements, moving inwards. :: s[i], s[end - i] = s[end - i], s[i] Code ---- .. literalinclude:: ../problems/easy/reverse-string/reverse_string__approach_1.py :language: python :lines: 14- Test ---- >>> from reverse_string__approach_1 import reverseString >>> s = ['h', 'e', 'l', 'l', 'o'] >>> reverseString(s) >>> s ['o', 'l', 'l', 'e', 'h'] >>> s = ['H', 'a', 'n', 'n', 'a', 'h'] >>> reverseString(s) >>> s ['h', 'a', 'n', 'n', 'a', 'H'] Complexity ---------- | :math:`n` is the length of the input string | Time: :math:`O(n)` — swap half the elements | Space: :math:`O(1)` .. autofunction:: reverse_string__approach_1.reverseString