When tackling strings in programming, one enduring challenge is reversing the words in a string – that is, turning the order of the words upside down while keeping the words themselves unchanged. For instance, transforming "Hello World"
into "World Hello"
may seem straightforward, but there are multiple ways to achieve this, each with its pros and cons. Let’s dive in!
At first glance, reversing words might simply mean changing their arrangement. However, we need to account for the spacing between words, potential trailing or leading spaces, and ensuring the words themselves do not get altered.
" Hello World "
"World Hello"
In this case, notice how we maintain only a single space between the reversed words and trim any leading or trailing spaces.
A straightforward approach to solve this problem is to utilize built-in string methods available in languages like Python.
split()
method to divide the string into words. By default, this will handle multiple spaces for us.join()
method to concatenate the reversed list into a single string.def reverse_words_simple(s): return ' '.join(s.split()[::-1]) # Test the function input_string = " Hello World " output_string = reverse_words_simple(input_string) print(output_string) # Output: "World Hello"
Pros:
Cons:
If you're looking to avoid built-in functions, the two-pointer technique offers a great alternative. This method iterates through the string while manipulating the characters directly.
def reverse_words_two_pointers(s): # Step 1: Trim the string s = s.strip() char_list = list(s) # Step 2: Reverse the entire string char_list.reverse() # Step 3: Reverse individual words start = 0 for end in range(len(char_list)): if char_list[end] == ' ': # Reverse the current word reverse_word(char_list, start, end - 1) start = end + 1 # Reverse the last word reverse_word(char_list, start, len(char_list) - 1) # Step 4: Build the result string return ''.join(char_list).strip() def reverse_word(char_list, left, right): while left < right: char_list[left], char_list[right] = char_list[right], char_list[left] left += 1 right -= 1 # Test the function input_string = " Hello World " output_string = reverse_words_two_pointers(input_string) print(output_string) # Output: "World Hello"
Pros:
Cons:
Using a stack is another intriguing method where we exploit the Last In First Out (LIFO) property to achieve the reversal.
def reverse_words_stack(s): stack = [] word = [] # Push words to the stack for char in s: if char != ' ': word.append(char) else: if word: stack.append(''.join(word)) word = [] if word: stack.append(''.join(word)) # Build the reversed string result = [] while stack: result.append(stack.pop()) return ' '.join(result) # Test the function input_string = " Hello World " output_string = reverse_words_stack(input_string) print(output_string) # Output: "World Hello"
Pros:
Cons:
By exploring these various methods, you gain a nuanced understanding of how to tackle the problem of reversing words in a string. Each approach carries its own set of trade-offs regarding readability, efficiency, and complexity, so consider the context in which you'll apply them.
13/10/2024 | DSA
23/09/2024 | DSA
16/11/2024 | DSA
15/11/2024 | DSA
06/12/2024 | DSA
23/09/2024 | DSA
06/12/2024 | DSA
06/12/2024 | DSA
15/11/2024 | DSA
06/12/2024 | DSA
15/11/2024 | DSA
23/09/2024 | DSA