
Problem
Solution
Submissions
Reverse String
Certification: Basic Level
Accuracy: 0%
Submissions: 0
Points: 5
Write a C program to reverse a string. The input string is given as an array of characters. You must do this by modifying the input array in-place with O(1) extra memory. Do not allocate extra space for another array.
You must modify the input array in-place and perform the reversal operation with constant extra memory.
Example 1
- Input: s = ["h","e","l","l","o"]
- Output: ["o","l","l","e","h"]
- Explanation: We start with ["h","e","l","l","o"], swap first and last characters: ["o","e","l","l","h"], swap second and second-to-last characters: ["o","l","l","e","h"]. Third character remains in place.
Example 2
- Input: s = ["H","a","n","n","a","h"]
- Output: ["h","a","n","n","a","H"]
- Explanation: We start with ["H","a","n","n","a","h"], then swap characters from the outside in: first and last, second and second-to-last, third and third-to-last.
Constraints
- 1 ≤ s.length ≤ 10^5
- s[i] is a printable ASCII character
- You must modify the array in-place with O(1) extra memory
- Time Complexity: O(n) where n is the length of the string
- Space Complexity: O(1)
Editorial
My Submissions
All Solutions
Lang | Status | Date | Code |
---|---|---|---|
You do not have any submissions for this problem. |
User | Lang | Status | Date | Code |
---|---|---|---|---|
No submissions found. |
Solution Hints
- Use a two-pointer approach to solve this problem
- Initialize one pointer at the beginning of the array and another at the end
- Swap the characters at both pointers
- Move the left pointer one step right and the right pointer one step left
- Continue until the pointers meet or cross each other