reverse the string best optimal solution

 Reverse a String

 you are given a string s. you need to reverse the string s .

input : aayush

output : hsuyaa

Your Task:

You only need to complete the function reverseWord() that takes s as parameter and returns the reversed string.

Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(1).

Constraints:
1 <= |s| <= 10000




solution: 

void swap(int &a , int &b)
{
    int temp ;
    temp = a ; 
    a = b ;
    b = temp;
}

string reverseWord(string str){
    
  int len = str.length();
  int x = len-1;
  int i =0;
  while(i < len/2)
  { 
      swap(str[i] , str[x]);
      i++;
      x--;
      
  }
  return str;
}

time complexity : O(n/2)

Comments