Reverse a String
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
Post a Comment