Skip to content

Instantly share code, notes, and snippets.

@sAVItar02
Created November 5, 2024 03:04
Show Gist options
  • Save sAVItar02/150cdb4e7e8ff1e3f18c6f83104f6bcf to your computer and use it in GitHub Desktop.
Save sAVItar02/150cdb4e7e8ff1e3f18c6f83104f6bcf to your computer and use it in GitHub Desktop.
Reverse String
/**
* @param {character[]} s
* @return {void} Do not return anything, modify s in-place instead.
*/
var reverseString = function(s) {
let start = 0;
let end = s.length - 1;
let temp = "";
while(start <= end) {
temp = s[start];
s[start] = s[end];
s[end] = temp;
start++;
end--;
}
};
// Two pointers and a temp variable approach
// Start poniter points to the start of the array and the end points at the end
// while start <= end swap the characters
// Time: O(n)
// Space: O(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment