Reverse String

Easy
2.

Reverse String

Reverse String is an easy coding problem on strings and two pointers, asked in Amazon, Microsoft, TCS and Infosys interviews.

Write a function that reverses a string and returns the result.

The classic form of this question hands you a character array and asks you to reverse it in place with O(1) extra memory; think in those terms even though this version returns a new string.

Example 1

Input
s = "hello"
Output
olleh

Example 2

Input
s = "Hannah"
Output
hannaH

Example 3

Input
s = "a"
Output
a

Constraints

  • 1 <= s.length <= 40
  • s consists of printable ASCII letters.
Write your solution here. Running it against the tests needs a free account — what you type comes with you.Sign in to run
/**
 * @param {string} s
 * @return {string}
 */
var reverseString = function(s) {
    // Write your code here
    
};

Sign in to run your own test cases

Sign in

Reverse String — Solution in Python, Java & C++

Reverse String solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem.

Reverse String is an easy coding problem on strings and two pointers, asked in Amazon, Microsoft, TCS and Infosys interviews.

Problem statement

Write a function that reverses a string and returns the result.

The classic form of this question hands you a character array and asks you to reverse it in place with O(1) extra memory; think in those terms even though this version returns a new string.

Example 1

Input: s = "hello"
Output: olleh

Example 2

Input: s = "Hannah"
Output: hannaH

Example 3

Input: s = "a"
Output: a

Constraints

  • 1 <= s.length <= 40
  • s consists of printable ASCII letters.

Hints for Reverse String

  1. Hint 1

    Two pointers, one at each end, swapping and stepping inward.

  2. Hint 2

    Stop when they meet; a middle character in an odd-length string swaps with itself and needs no special case.

How to solve Reverse String

Two pointers, one at each end, swap and step inward until they meet.

Approach

  1. Copy the string into a mutable character array.
  2. With left = 0 and right = n - 1, swap the two characters and move both pointers inward.
  3. Stop when left >= right and join the array.

Why it works

Each swap places two characters in their final mirrored positions; after ⌊n/2⌋ swaps every character has been exchanged with its mirror. A middle character in an odd-length string is already in place.

Complexity

  • Time — O(n)
  • Space — O(n) for the copy — O(1) extra in the in-place array form

Pitfalls

  • Using <= as the loop condition swaps the middle character with itself — harmless, just wasteful.
  • The classic form mutates an array; a language-level reverse is fine here but is not the technique being tested.

Reference solution

Python

def reverseString(s: str) -> str:
    chars = list(s)
    left, right = 0, len(chars) - 1
    while left < right:
        chars[left], chars[right] = chars[right], chars[left]
        left += 1
        right -= 1
    return "".join(chars)

Java

public static String reverseString(String s) {
    char[] chars = s.toCharArray();
    int left = 0, right = chars.length - 1;
    while (left < right) {
        char t = chars[left];
        chars[left] = chars[right];
        chars[right] = t;
        left++;
        right--;
    }
    return new String(chars);
}

C++

string reverseString(string s) {
    int left = 0, right = (int) s.size() - 1;
    while (left < right) {
        char t = s[left];
        s[left] = s[right];
        s[right] = t;
        left++;
        right--;
    }
    return s;
}

JavaScript

var reverseString = function(s) {
    const chars = s.split("");
    let left = 0, right = chars.length - 1;
    while (left < right) {
        const t = chars[left];
        chars[left] = chars[right];
        chars[right] = t;
        left++;
        right--;
    }
    return chars.join("");
};

Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.

All 515 strings problems · the whole catalogue

Learn the technique: Strings · Two Pointers Technique