Reverse a String

Given a string s, return a new string containing all its characters in reverse order.

Preserve every character exactly, including uppercase and lowercase letters, digits, spaces, and special characters. Do not remove spaces, change character case, or normalize the input. The original input string must remain unchanged.

Example 1:

Input: s = "hello"

Output: "olleh"

Explanation: Reading the original characters from last to first gives o, l, l, e, and h. Combining them produces olleh. The original string remains hello.

Example 2:

Input: s = "AbC123"

Output: "321CbA"

Explanation: The digits 1, 2, 3 appear first in reverse order as 3, 2, 1, followed by C, b, A. Each letter keeps its original case, so the new string is 321CbA.

Now Your Turn!

Pick the correct output for the given input

Input: s = "a b!"

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 0 <= s.length() <= 105
  • s contains printable ASCII characters (codes 32–126), including spaces.

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
class Solution {
public:
string reverseString(string s) {
// Write your code here
}
};
 
Test Case

Input:

S