Introduction

The Reverse String problem involves reversing the order of characters in a string.

Given a string s, the task is to:

  • reverse all characters
  • return the reversed string

This problem helps in understanding:

  • string traversal
  • swapping techniques
  • Two Pointer Technique
  • in-place modification

Example

Input:s = "hello"

Output:
"olleh"
Explanation:
Characters are reversed from end to beginning.
Input: s = "coding"
Output:
"gnidoc"
Explanation:
First character becomes last
and last character becomes first.

Constraints

 1 <= s.length <= 10^5 s contains printable ASCII characters

Approach 1 : Brute Force

Explanation

The simplest way to solve this problem is:

  1. Traverse the string from end
  2. Append characters into a new string

This approach is easy to understand but uses extra space.

Steps

  1. Create empty result string.
  2. Traverse original string from end.
  3. Append characters into result.
  4. Return reversed string.

Dry Run

Input:s = "hello"

Traverse from end:
o → result = "o"
l → result = "ol"
l → result = "oll"
e → result = "olle"
h → result = "olleh"
Final Result:
"olleh"

Brute Force Code

Complexity Analysis

Time Complexity: O(n)Explanation:
Each character is traversed once.
Space Complexity: O(n)
Explanation:
Extra result string is used.

Approach 2 : Optimized Solution (Two Pointer Technique)

Explanation

The optimized solution uses the Two Pointer Technique.

The idea is:

  1. Place:
    • left pointer at beginning
    • right pointer at end
  2. Swap characters.
  3. Move pointers toward center.

This reverses the string efficiently.

Steps

  1. Convert string into character array.
  2. Initialize:
    • left = 0
    • right = n - 1
  3. Swap characters.
  4. Move pointers inward.
  5. Return reversed string.

Dry Run

Input:s = "hello"

Initially:
left = 0 → h
right = 4 → o

Swap:
o e l l h
Move pointers
left = 1 → e
right = 3 → l
Swap:
o l l e h
Pointers cross
Final Result:
"olleh"

Optimized Code

Complexity Analysis

Time Complexity: O(n)Explanation:
Each character is processed once.

Space Complexity: O(n)
Explanation: Reversal is performed in-place.

Edge Cases

  1. String contains one character
  2. String contains spaces
  3. String contains special characters
  4. String already palindrome
  5. Empty-like minimal input

Why This Problem is Important

This problem helps in understanding:

  1. String traversal
  2. Two Pointer Technique
  3. Swapping logic
  4. In-place modification
  5. Character manipulation

It is one of the most common beginner-level string interview problems.

Real-World Applications

String reversal concepts are used in:

  1. Text processing systems
  2. Compiler design
  3. Encryption systems
  4. Data formatting
  5. Parsing engines

Common Mistakes

  1. Incorrect pointer updates
  2. Wrong loop condition
  3. Forgetting string immutability
  4. Incorrect swap logic

Interview Tips

Interviewers often expect:

  1. Two Pointer optimization
  2. Proper swapping explanation
  3. In-place reversal logic

Always explain why two pointers efficiently reverse the string.

Related Questions

  1. Palindrome
  2. Reverse Words
  3. Valid Anagram
  4. Remove Spaces
  5. String Compression

Final Takeaway

The Reverse String problem is a fundamental string manipulation problem that teaches traversal and Two Pointer swapping techniques. Understanding this problem builds a strong foundation for advanced string processing and interview problems.