{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "<h1>344. Reverse String</h1>\n", "<hr>\n", "\n", "<!--Copy Paste Leetcode statement between-->\n", "<p>Write a function that reverses a string. The input string is given as an array of characters <code>char[]</code>.</p>\n", "\n", "<p>Do not allocate extra space for another array, you must do this by <strong>modifying the input array <a href=\"https://en.wikipedia.org/wiki/In-place_algorithm\" target=\"_blank\">in-place</a></strong> with O(1) extra memory.</p>\n", "\n", "<p>You may assume all the characters consist of <a href=\"https://en.wikipedia.org/wiki/ASCII#Printable_characters\" target=\"_blank\">printable ascii characters</a>.</p>\n", "\n", "<p> </p>\n", "\n", "<div>\n", "<p><strong>Example 1:</strong></p>\n", "\n", "<pre><strong>Input: </strong><span id=\"example-input-1-1\">[\"h\",\"e\",\"l\",\"l\",\"o\"]</span>\n", "<strong>Output: </strong><span id=\"example-output-1\">[\"o\",\"l\",\"l\",\"e\",\"h\"]</span>\n", "</pre>\n", "\n", "<div>\n", "<p><strong>Example 2:</strong></p>\n", "\n", "<pre><strong>Input: </strong><span id=\"example-input-2-1\">[\"H\",\"a\",\"n\",\"n\",\"a\",\"h\"]</span>\n", "<strong>Output: </strong><span id=\"example-output-2\">[\"h\",\"a\",\"n\",\"n\",\"a\",\"H\"]</span>\n", "</pre>\n", "</div>\n", "</div>\n", "<!--Copy Paste Leetcode statement between-->\n", "\n", "<p> </p>\n", "<a href=\"https://leetcode.com/problems/reverse-string/\">Source</a> \n", "<hr>\n", "\n", "<h4>Code</h4>" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "def reverse_list(stringlist):\n", " \"\"\"Iterative approach\"\"\"\n", " if not stringlist:\n", " return\n", " left = 0\n", " right = len(stringlist)-1\n", " while left < right:\n", " stringlist[left], stringlist[right] = stringlist[right], stringlist[left]\n", " left += 1\n", " right -= 1\n", " return stringlist" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "def reverse_list(stringlist):\n", " \"\"\"Iterative approach\"\"\"\n", " n = len(stringlist)\n", " # base case\n", " if n <= 1:\n", " return stringlist\n", " # recursion\n", " return(reverse_list(stringlist[n//2:]) + reverse_list(stringlist[:n//2]))" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "def reverse_list(stringlist):\n", " \"\"\"cheating\"\"\"\n", " mylist[:] = mylist[::-1] # modified in place\n", " # return mylist[::-1] # NOT modified in place" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "<h4>Check</h4>" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['o', 'l', 'l', 'e', 'h']" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "stringlist = [\"h\",\"e\",\"l\",\"l\",\"o\"]\n", "reverse_list(stringlist)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['h', 'a', 'n', 'n', 'a', 'H']" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "stringlist = [\"H\",\"a\",\"n\",\"n\",\"a\",\"h\"]\n", "reverse_list(stringlist)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.2" } }, "nbformat": 4, "nbformat_minor": 1 }