Problem
Reverse text so the last character comes first.
Implement reverse_string(text).
Input
text: The text to reverse.
Output
Return the reversed text.
Constraints
0 ≤ length ≤ 10,000
Examples
Input:
text = "hello"
Expected output:
expected = "olleh"
Input:
std::string text = "hello";
Expected output:
std::string expected = "olleh";
Input:
string text = "hello";
Expected output:
string expected = "olleh";
Explanation
h-e-l-l-o read backwards is "olleh".
Input:
text = "a"
Expected output:
expected = "a"
Input:
std::string text = "a";
Expected output:
std::string expected = "a";
Input:
string text = "a";
Expected output:
string expected = "a";
Explanation
A single character reversed is itself.
How to approach it
Try on your own first, then open one hint at a time.
Hint 1
The answer is the characters in opposite order.
Hint 2
A two-pointer approach or a backward traversal both work well.
Hint 3
Be sure the empty string stays empty after reversal.
Expected complexity
O(n) time, O(n) extra space
References
This challenge is adapted for CodeUtility. Follow the statement and constraints above.