You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
🔥 Reverse String 🔥 || 3 Approaches || Simple Fast and Easy || with Explanation
Solution - 1
classSolution {
voidsolve(List<String> s, int left, int right) {
if (left >= right) return;
String temp = s[left];
s[left] = s[right];
s[right] = temp;
solve(s, ++left, --right);
}
voidreverseString(List<String> s) {
int left =0;
int right = s.length -1;
solve(s, left, right);
}
}
Solution - 2
classSolution {
voidreverseString(List<String> s) {
int left =0, right = s.length -1;
while (left < right) {
String temp = s[right];
s[right--] = s[left];
s[left++] = temp;
}
}
}
Solution - 3
classSolution {
voidreverseString(List<String> s) {
//create a stackList<String> st = [];
//loop through the string, to add all the string elements in stackfor (int i =0; i < s.length; i++) {
//create a variable to add the valuesString ch = s[i];
st.add(ch);
}
//empty the original string
s.clear();
//add the stack elements to the stringwhile (!st.isEmpty) {
String ch = st.first;
//reversed
s.add(ch);
st.removeLast();
}
}
}