Reverse individual words with O ( 1 ) extra space | Function to resturn the after reversing the individual words ; Pointer to the first character of the first word ; If the current word has ended ; Pointer to the last character of the current word ; Reverse the current word ; Pointer to the first character of the next word ; Driver code
def reverseWords ( Str ) : NEW_LINE INDENT start = 0 NEW_LINE for i in range ( len ( Str ) ) : NEW_LINE INDENT if ( Str [ i ] == ' ▁ ' or i == len ( Str ) - 1 ) : NEW_LINE INDENT end = i - 1 NEW_LINE if ( i == len ( Str ) - 1 ) : NEW_LINE INDENT end = i NEW_LINE DEDENT while ( start < end ) : NEW_LINE INDENT Str [ start ] , Str [ end ] = Str [ end ] , Str [ start ] NEW_LINE start += 1 NEW_LINE end -= 1 NEW_LINE DEDENT start = i + 1 NEW_LINE DEDENT DEDENT return " " . join ( Str ) NEW_LINE DEDENT Str = " Geeks ▁ for ▁ Geeks " NEW_LINE Str = [ i for i in Str ] NEW_LINE print ( reverseWords ( Str ) ) NEW_LINE