From 10e16b97e26f3a8261d885018df208bfc81b8357 Mon Sep 17 00:00:00 2001 From: Sangeeth Sudheer Date: Fri, 1 Apr 2022 17:23:23 +0530 Subject: [PATCH] feat(0344-reverse-string): add py3 solution --- 0344_reverse-string/README.md | 16 ++++++++++++++++ 0344_reverse-string/python3/solution.py | 13 +++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 0344_reverse-string/README.md create mode 100644 0344_reverse-string/python3/solution.py diff --git a/0344_reverse-string/README.md b/0344_reverse-string/README.md new file mode 100644 index 0000000..214a9de --- /dev/null +++ b/0344_reverse-string/README.md @@ -0,0 +1,16 @@ +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](https://en.wikipedia.org/wiki/In-place_algorithm) 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 <= 105` +* `s[i]` is a [printable ascii character](https://en.wikipedia.org/wiki/ASCII#Printable_characters). diff --git a/0344_reverse-string/python3/solution.py b/0344_reverse-string/python3/solution.py new file mode 100644 index 0000000..f41b617 --- /dev/null +++ b/0344_reverse-string/python3/solution.py @@ -0,0 +1,13 @@ +class Solution: + def reverseString(self, s: List[str]) -> None: + """ + Do not return anything, modify s in-place instead. + """ + i = 0 + j = len(s) - 1 + + while i < j: + s[i], s[j] = s[j], s[i] + i += 1 + j -= 1 + \ No newline at end of file