-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverseString.js
More file actions
62 lines (48 loc) · 1.38 KB
/
Copy pathreverseString.js
File metadata and controls
62 lines (48 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// /**
// * @param {character[]} s
// * @return {void} Do not return anything, modify s in-place instead.
// */
let s = ["h","e","l","l","o"]
/**
* @param {character[]} s
* @return {void} Do not return anything, modify s in-place instead.
*/
// var reverseString = function(s) {
// let left = 0;
// let right = s.length - 1;
// while(left < right){
// let temp = s[left];
// s[left] = s[right];
// s[right] = temp
// left++;
// right--;
// }
// };
// This is 2 pointer way solve this problem
var reverseString = function(s) {
let left = 0;
let right = s.length - 1;
while(left < right){
let temp = s[left];
s[left] = s[right];
s[right] = temp;
left++;
right--;
}
};
// 1. Create an array of characters
let myArray = ['h', 'e', 'l', 'l', 'o'];
// 2. Call the function (modifies myArray directly)
reverseString(myArray);
// 3. Print the modified array to the console
console.log(myArray);
// Output: [ 'o', 'l', 'l', 'e', 'h' ]
// 4. (Optional) Print it back as a single string
console.log(myArray.join(''));
// Output: "olleh"
// Time Complexity: ✅ O(n)
// Even though the loop runs n/2 times, we ignore constants in Big O.
// So O(n/2) becomes O(n).
// Space Complexity: ✅ O(1)
// Only three extra variables are used: left, right, and temp.
// No extra array is created.