Reverse individual words with O(1) extra space
Given a string str, the task is to reverse all the individual words.
Examples:
Input: str = “Hello World”
Output: olleH dlroW
Input: str = “Geeks for Geeks”
Output: skeeG rof skeeG
Approach: A solution to the above problem has been discussed in this post. It has a time complexity of O(n) and uses O(n) extra space. In this post, we will discuss a solution which uses O(1) extra space.
- Traverse through the string until we encounter a space.
- After encountering the space, we use two variables ‘start’ and ‘end’ pointing to the first and last character of the word and we reverse that particular word.
- Repeat the above steps until the last word.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to resturn the string after // reversing the individual words string reverseWords(string str) { // Pointer to the first character // of the first word int start = 0; for (int i = 0; i <= str.size(); i++) { // If the current word has ended if (str[i] == ' ' || i == str.size()) { // Pointer to the last character // of the current word int end = i - 1; // Reverse the current word while (start < end) { swap(str[start], str[end]); start++; end--; } // Pointer to the first character // of the next word start = i + 1; } } return str; } // Driver code int main() { string str = "Geeks for Geeks"; cout << reverseWords(str); return 0; } |
Java
// Java implementation of the approach class GFG { static String swap(String str, int i, int j) { StringBuilder sb = new StringBuilder(str); sb.setCharAt(i, str.charAt(j)); sb.setCharAt(j, str.charAt(i)); return sb.toString(); } // Function to resturn the string after // reversing the individual words static String reverseWords(String str) { // Pointer to the first character // of the first word int start = 0; for (int i = 0; i < str.length(); i++) { // If the current word has ended if (str.charAt(i) == ' ' || i == str.length()-1 ) { // Pointer to the last character // of the current word int end; if (i == str.length()-1) end = i ; else end = i - 1 ; // Reverse the current word while (start < end) { str = swap(str,start,end) ; start++; end--; } // Pointer to the first character // of the next word start = i + 1; } } return str ; } // Driver code public static void main(String args[]) { String str = "Geeks for Geeks"; System.out.println(reverseWords(str)); } } // This code is contributed by AnkitRai01 |
Python3
# Python3 implementation of the approach # Function to resturn the after # reversing the individual words def reverseWords(Str): # Pointer to the first character # of the first word start = 0 for i in range(len(Str)): # If the current word has ended if (Str[i] == ' ' or i == len(Str)-1): # Pointer to the last character # of the current word end = i - 1 if(i == len(Str)-1): end = i # Reverse the current word while (start < end): Str[start], Str[end]=Str[end],Str[start] start+=1 end-=1 # Pointer to the first character # of the next word start = i + 1 return "".join(Str) # Driver code Str = "Geeks for Geeks"Str=[i for i in Str] print(reverseWords(Str)) # This code is contributed by mohit kumar 29 |
C#
// C# implementation of the approach using System; class GFG { // Function to resturn the string after // reversing the individual words // Function to resturn the string after // reversing the individual words static String reverseWords(String str) { // Pointer to the first character // of the first word int start = 0; for (int i = 0; i < str.Length; i++) { // If the current word has ended if (str[i] == ' ' || i == str.Length-1 ) { // Pointer to the last character // of the current word int end; if (i == str.Length-1) end = i ; else end = i - 1 ; // Reverse the current word while (start < end) { str = swap(str,start,end) ; start++; end--; } // Pointer to the first character // of the next word start = i + 1; } } return str ; } static String swap(String str, int i, int j) { char []ch = str.ToCharArray(); char temp = ch[i]; ch[i] = ch[j]; ch[j] = temp; return String.Join("",ch); } // Driver code public static void Main(String []args) { String str = "Geeks for Geeks"; Console.WriteLine(reverseWords(str)); } } /* This code is contributed by PrinciRaj1992 */ |
skeeG rof skeeG
Time Complexity: O(n)
Auxiliary Space: O(1)
Recommended Posts:
- Reverse individual words
- Clone a stack without extra space
- Replace all occurrences of string AB with C without using extra space
- Remove duplicates from a string in O(1) extra space
- Check if the characters in a string form a Palindrome in O(1) extra space
- Sum of all substrings of a string representing a number | Set 2 (Constant Extra Space)
- Reverse words in a given string
- Reverse middle words of a string
- Reverse words in a given String in Java
- Reverse String according to the number of words
- Reverse words in a given String in Python
- Print words of a string in reverse order
- Reverse a string preserving space positions
- Check if the given string of words can be formed from words present in the dictionary
- Count words that appear exactly two times in an array of words
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



