DESCRIPTION:
Write a function that reverses a string. The input string is given as an array of characters s.
You must do this by modifying the input array in-place with O(1) extra memory.
Example 1:
Input: s = ["h","e","l","l","o"]
Output: ["o","l","l","e","h"]
Example 2:
Input: s = ["H","a","n","n","a","h"]
Output: ["h","a","n","n","a","H"]
Constraints:
1 <= s.length <= 105
s[i] is a printable ascii character.
1.STRING GIVEN EG: RUPA
2.CHAR ARRAY GIVEN EG: ["R","U","P","A"]
1.STRING GIVEN EG: RUPA
SOLUTION C++:
#include <bits/stdc++.h>
using namespace std;
void reverse(string s){
for(int i=s.length()-1; i>=0; i--)
cout<<s[i];
}
int main(){
string s = "Rupa";
reverse(s);
return 0;
}
2.2.CHAR ARRAY GIVEN EG: "R","U","P","A"
SOLUTION C++;
class Solution {
public:
void reverseString(vector<char>& s) {
int n=s.size();
int l=0;
int r=n-1;
while(l<=r){
swap(s[l],s[r]);
l++;
r--;
}
}
};
Top comments (0)