Reverse String

Easy · Backtracking · Two Pointers · String · Recursion

Problem

You are given a string as a list of characters, s. Reverse the order of the characters in place: change the list itself using only O(1) extra memory, and return nothing.

The expected output shows the contents of s after your function runs.

Examples

Input: s = ["c","o","d","e"]
Output: ["e","d","o","c"]

The first and last characters swap, then the middle two.

Input: s = ["a","b","a","c"]
Output: ["c","a","b","a"]

Read backwards, the list is c, a, b, a.

Input: s = ["l","o","t","u","s"]
Output: ["s","u","t","o","l"]

l and s swap, o and u swap, and the middle t stays where it is.

Constraints

Approaches

Follow-up

What if only some characters may move, such as letters, while digits and punctuation stay put? The same two pointers work if each one skips the characters that stay. Both use cases below ask a version of that.

Related problems

Start free on Cariara · Coding problems · System design studio

Starting Python
No results yet.

          
↑↓ move ↵ open esc close

Labs

Loading labs…

Run with your own input

Starting Python

Loading Python…