CSInterviewing
Problems
Register
Login
Problems
Register
Login
menu
Reverse String
string
twopointers
# Reverse String ## Difficulty Easy ## Tags `string`, `two pointers` ## Problem Statement Write a function that reverses a string. The input string is given as an array of characters `s`. You must do this by modifying the input array **in-place** with O(1) extra memory. ## Constraints - 1 ≤ s.length ≤ 10⁵ - `s[i]` is a printable ASCII character. ## Signatures ### Python ```python def reverse_string(s: List[str]) -> None: pass ``` ### Java ```java public class Solution { public void reverse_string(List<String> s) { return null; } } ``` ### Csharp ```csharp public class Solution { public void reverse_string(IList<string> s) { throw new NotImplementedException(); } } ``` ### Cpp ```cpp class Solution { public: void reverse_string(vector<string> s) { } }; ``` ### C ```c void reverse_string(vector<char*> s) { } ``` ### Ruby ```ruby def reverse_string(s) end ``` ### Perl ```perl sub reverse_string { my ($self, @args) = @_; } ``` ### Scala ```scala object Solution { def reverse_string(s: List[String]): Unit = { } } ``` ### Haskell ```haskell reverse_string :: ... -> ... reverse_string args = undefined ``` ### Clojure ```clojure (defn reverse_string [s] ) ``` ### Scheme ```scheme (define (reverse_string s) ) ``` ## Examples ### Example 1 **Input:** ``` s = ["h", "e", "l", "l", "o"] ``` **Output:** ``` ["o", "l", "l", "e", "h"] ``` ### Example 2 **Input:** ``` s = ["H", "a", "n", "n", "a", "h"] ``` **Output:** ``` ["h", "a", "n", "n", "a", "H"] ``` ### Example 3 **Input:** ``` s = ["a"] ``` **Output:** ``` ["a"] ``` ## Hints 1. Use two pointers: one at the start, one at the end. 2. Swap elements at both pointers. 3. Move pointers toward each other until they meet.
Login required
Please log in to view the solution editor and submit code.
Login
Register