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.
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"]
Constraints:
1 <= s.length <= 105s[i] is a printable ascii character.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The most straightforward way to reverse a string is to create a brand new, empty string. Then, we can build the reversed version by adding characters to this new string one by one, starting from the end of the original string.
Here's how the algorithm would work step-by-step:
def reverse_string_brute_force(original_string):
# Prepare a new container to build the reversed string piece by piece.
reversed_characters = []
# Iterate backwards through the original string to access characters in reverse order.
for character_index in range(len(original_string) - 1, -1, -1):
character_to_append = original_string[character_index]
reversed_characters.append(character_to_append)
# Join the collected characters to form the final reversed string.
return "".join(reversed_characters)The most efficient way to reverse a sequence of characters is to swap pairs of characters from opposite ends, gradually working inward. This avoids creating a new copy and modifies the original sequence directly.
Here's how the algorithm would work step-by-step:
def reverse_string(string_as_list):
"""
Do not return anything, modify string_as_list in-place instead.
"""
left_pointer = 0
right_pointer = len(string_as_list) - 1
# Iterate until the two pointers meet or cross, ensuring the entire string is processed.
while left_pointer < right_pointer:
# Swap the characters at the outer ends to reverse their positions.
temp_char_holder = string_as_list[left_pointer]
string_as_list[left_pointer] = string_as_list[right_pointer]
string_as_list[right_pointer] = temp_char_holder
# Move the pointers inward to process the next pair of characters.
left_pointer += 1
right_pointer -= 1| Case | How to Handle |
|---|---|
| An empty string | The algorithm correctly does nothing as the loop condition for swapping will not be met. |
| A string with a single character | The algorithm correctly does nothing as the pointers will start at the same position. |
| A string that is a palindrome | The algorithm will perform swaps but the resulting string will be identical to the original. |
| A string with all identical characters | The algorithm performs swaps, but since all characters are the same, the string remains unchanged. |
| A very long string approaching system memory limits | The in-place algorithm has O(1) space complexity, making it highly efficient for large inputs without extra memory allocation. |
| A string containing non-alphanumeric characters or symbols | The algorithm treats all characters equally, swapping them based on their position regardless of their type. |
| A string with an even number of characters | The two pointers will cross after the final swap in the middle, correctly terminating the reversal. |
| A string with an odd number of characters | The two pointers will meet at the middle character, which is not swapped, and the loop terminates correctly. |