Question is saying do not return anything that means you need to do inplace reversing. Also this is list of string not a string so whatever you change inside function it will be reflected in the list because lists are mutable.

Correct Solution will be

class Solution:
    def reverseString(self, s: List[str]) -> None:
        """
        Do not return anything, modify s in-place instead.
        """
        s.reverse()

Also If you want to do in your style then correct line will be

s[:] = s[::-1]
Answer from Deepak Tripathi on Stack Overflow
๐ŸŒ
LeetCode
leetcode.com โ€บ problems โ€บ reverse-string
Reverse String - LeetCode
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.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ reversing a string leetcode question
r/learnpython on Reddit: reversing a string LeetCode question
May 9, 2023 -

So the code goes:

s = [โ€œhโ€, โ€eโ€, โ€lโ€, โ€lโ€, โ€oโ€]

s[:] = s[::-1] print(s)

This prints the reverse of s in both VSCode and on the LeetCode website ide

I tried to see if the follow would work:

s = [โ€œhโ€, โ€eโ€, โ€lโ€, โ€lโ€, โ€oโ€]

s = s[::-1] print(s)

This did work but only on VSCode not on leet code. So my question is why did it work in VSCode and not LeetCode?

Thereโ€™s other solutions I tried to the problem that work in VSCode but not in LeetCode shock I find it odd, am I doing something I shouldn't?

๐ŸŒ
LeetCode
leetcode.com โ€บ problems โ€บ reverse-string-ii
Reverse String II - LeetCode
Reverse String II - Given a string s and an integer k, reverse the first k characters for every 2k characters counting from the start of the string. If there are fewer than k characters left, reverse all of them.
๐ŸŒ
DEV Community
dev.to โ€บ ranggakd โ€บ reverse-string-leetcode-python-418m
Reverse String | LeetCode | Python - DEV Community
May 17, 2023 - class Solution: def reverseString(self, s: List[str]) -> None: l = len(s) for i in range(l//2): if s[i] != s[l-1-i]: s[i], s[l-1-i] = s[l-1-i], s[i]
๐ŸŒ
AlgoMonster
algo.monster โ€บ liteproblems โ€บ 344
344. Reverse String - In-Depth Explanation
In-depth solution and explanation for LeetCode 344. Reverse String in Python, Java, C++ and more. Intuitions, example walk through, and complexity analysis. Better than official and forum solutions.
๐ŸŒ
AlgoMap
algomap.io โ€บ problems โ€บ reverse-string
Reverse String - Leetcode Solution
A naive approach is to use a temporary array to store the reversed characters and then copy them back into the original array. Create a new array T to store the characters in reverse order. Loop from the end of s to the beginning, appending each character to T.
Top answer
1 of 7
4

To do this without space, you need to swap. You can't add array slices. Instead of splitting the indexes in the middle, which will never let you swap opposite pairs (expect in the base case).

You can see it, if you imagine the recursion visually. You start with a list like:

1, 2, 3, 4
^        ^ <-- these need to swap in a reverse

But after your first recursive call you split this into:

---- | ----
1, 2   3, 4
^         ^  <-- these still need to be swapped, bu when?

Now branch one has no way to get at the 4 in branch two to swap unless there's a non-obvious way to do it as the recursion unwinds.

You can instead (much more easily) walk you indexes in from both ends and swap as you go. Then your base case is just when they meet in the middle:

class Solution:
    def reverseString(self, s, lo=0, hi=None):
        if hi == None:
            hi = len(s) - 1

        if hi <= lo:
            return s

        s[lo], s[hi] = s[hi], s[lo]
        return self.reverseString(s, lo + 1, hi - 1)


s = Solution()
s.reverseString([1, 2, 3, 4])
# [4, 3, 2, 1]
s.reverseString([1, 2, 3, 4, 5])
#[5, 4, 3, 2, 1]
2 of 7
1

I am not sure why are you doing recursion. You can simply take two pointers, one at the start and one at the end of the string, start by swapping those characters, and move the pointers towards each other, until they cross, and then you break and return the reversed string.

class Solution:
    def reverseString(self, s):

        if len(s) <= 1: return s
        # The two pointers
        lo = 0
        hi = len(s) - 1
        # Iterate till both pointers cross
        while lo < hi:
            # swap the characters
            tmp = s[lo]
            s[lo] = s[hi]
            s[hi] = tmp
            # increment the pointers
            lo += 1
            hi -= 1
        return s


s = Solution()
print(s.reverseString(['h']))
print(s.reverseString(["h","e","l","l","o"]))
print(s.reverseString(["h","e","l","l","o","w","o","r","l","d"]))
#['h']
#['o', 'l', 'l', 'e', 'h']
#['d', 'l', 'r', 'o', 'w', 'o', 'l', 'l', 'e', 'h']

In addition, the recursive approach for the same is as follows

class Solution:
    def reverseString(self, s, lo=0, hi=None):

        #If one character or less in the string, return the string
        if len(s) <= 1:
            return s

        #The last index should be placed at the end of the string
        if hi == None:
            hi = len(s) - 1

        #If the two indexes cross, return the string
        if hi < lo:
            return s

        #swap the low and high characters
        tmp = s[lo]
        s[lo] = s[hi]
        s[hi] = tmp
        #Recursively call the function
        return self.reverseString(s, lo + 1, hi - 1)

s = Solution()
print(s.reverseString(['h']))
print(s.reverseString(["h","e","l","l","o"]))
print(s.reverseString(["h","e","l","l","o","w","o","r","l","d"]))
#['h']
#['o', 'l', 'l', 'e', 'h']
['d', 'l', 'r', 'o', 'w', 'o', 'l', 'l', 'e', 'h']
Find elsewhere
๐ŸŒ
LeetCode
leetcode.com โ€บ problems โ€บ reverse-words-in-a-string
Reverse Words in a String - LeetCode
Can you solve this real interview question? Reverse Words in a String - Given an input string s, reverse the order of the words. A word is defined as a sequence of non-space characters.
๐ŸŒ
GitHub
github.com โ€บ kamyu104 โ€บ LeetCode-Solutions โ€บ blob โ€บ master โ€บ Python โ€บ reverse-string.py
LeetCode-Solutions/Python/reverse-string.py at master ยท kamyu104/LeetCode-Solutions
๐Ÿ‹๏ธ Python / Modern C++ Solutions of All 3822 LeetCode Problems (Weekly Update) - LeetCode-Solutions/Python/reverse-string.py at master ยท kamyu104/LeetCode-Solutions
Author ย  kamyu104
๐ŸŒ
Medium
lydiaplu.medium.com โ€บ leetcode-541-reverse-string-ii-24a879b40fdd
LeetCode โ€” 541. Reverse String II | by Pan Lu | Medium
January 31, 2024 - Reversing Characters: The characters between left and right are then reversed using a while loop and a temporary variable for swapping. Returning the Result: After all segments are processed, the character array is returned to a string.
๐ŸŒ
LeetCode
leetcode.com โ€บ problems โ€บ reverse-words-in-a-string-iii
Reverse Words in a String III - LeetCode
Can you solve this real interview question? Reverse Words in a String III - Given a string s, reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order. Example 1: Input: s = "Let's take LeetCode contest" Output: "s'teL ekat edoCteeL tsetnoc" Example 2: Input: s = "Mr Ding" Output: "rM gniD" Constraints: * 1
๐ŸŒ
Medium
medium.com โ€บ @dylanzenner โ€บ leetcode-daily-python-344-reverse-string-43efd8e5b005
LeetCode Daily (Python): # 344 Reverse String | by Dylan Zenner | Medium
January 6, 2024 - We then start a loop and basically tell our code to swap the character at the left_pointer with the character at the right_pointer then we update our pointers to progress through the string.
๐ŸŒ
AlgoMonster
algo.monster โ€บ liteproblems โ€บ 151
151. Reverse Words in a String - In-Depth Explanation
In-depth solution and explanation for LeetCode 151. Reverse Words in a String in Python, Java, C++ and more. Intuitions, example walk through, and complexity analysis. Better than official and forum solutions.
๐ŸŒ
GitHub
github.com โ€บ yizhou-wang โ€บ LeetCode โ€บ blob โ€บ master โ€บ Python โ€บ reverse-string.py
LeetCode/Python/reverse-string.py at master ยท yizhou-wang/LeetCode
:pencil: Python / C++ 11 Solutions of All LeetCode Questions - LeetCode/Python/reverse-string.py at master ยท yizhou-wang/LeetCode
Author ย  yizhou-wang
๐ŸŒ
AlgoMonster
algo.monster โ€บ liteproblems โ€บ 541
541. Reverse String II - In-Depth Explanation
In-depth solution and explanation for LeetCode 541. Reverse String II in Python, Java, C++ and more. Intuitions, example walk through, and complexity analysis. Better than official and forum solutions.