Skip to content

Commit 40960d2

Browse files
committed
0344: Reverse String
1 parent 0bcd899 commit 40960d2

File tree

3 files changed

+47
-34
lines changed

3 files changed

+47
-34
lines changed

0344-Reverse_String/main.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,13 @@ function reverseStringUsingTempVariable(s) {
2222
right--;
2323
}
2424
}
25-
reverseStringUsingDestructuring(strArray1);
26-
reverseStringUsingTempVariable(strArray2);
27-
console.log(strArray1);
28-
console.log(strArray2);
25+
let strArrayForDestructuring = [...strArray1];
26+
console.time("reverseStringUsingDestructuring");
27+
reverseStringUsingDestructuring(strArrayForDestructuring);
28+
console.timeEnd("reverseStringUsingDestructuring");
29+
let strArrayForTempVariable = [...strArray1];
30+
console.time("reverseStringUsingTempVariable");
31+
reverseStringUsingTempVariable(strArrayForTempVariable);
32+
console.timeEnd("reverseStringUsingTempVariable");
33+
console.log(strArrayForDestructuring);
34+
console.log(strArrayForTempVariable);

0344-Reverse_String/main.ts

+29-22
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,38 @@ let strArray1 = ["h", "e", "l", "l", "o"]
22
let strArray2 = ["H", "a", "n", "n", "a", "h"]
33

44
function reverseStringUsingDestructuring(s: string[]): void {
5-
let left = 0
6-
let right = s.length - 1
7-
while (left < right) {
8-
;[s[left], s[right]] = [s[right], s[left]]
9-
left++
10-
right--
11-
}
5+
let left = 0
6+
let right = s.length - 1
7+
while (left < right) {
8+
;[s[left], s[right]] = [s[right], s[left]]
9+
left++
10+
right--
11+
}
1212
}
1313

1414
function reverseStringUsingTempVariable(s: string[]): void {
15-
let length: number = s.length
16-
let left: number = 0,
17-
right: number = length - 1
18-
let tempStr: string
19-
while (left < right) {
20-
tempStr = s[left]
21-
s[left] = s[right]
22-
s[right] = tempStr
23-
left++
24-
right--
25-
}
15+
let length: number = s.length
16+
let left: number = 0,
17+
right: number = length - 1
18+
let tempStr: string
19+
while (left < right) {
20+
tempStr = s[left]
21+
s[left] = s[right]
22+
s[right] = tempStr
23+
left++
24+
right--
25+
}
2626
}
2727

28-
reverseStringUsingDestructuring(strArray1)
29-
reverseStringUsingTempVariable(strArray2)
28+
let strArrayForDestructuring = [...strArray1]
29+
console.time("reverseStringUsingDestructuring")
30+
reverseStringUsingDestructuring(strArrayForDestructuring)
31+
console.timeEnd("reverseStringUsingDestructuring")
3032

31-
console.log(strArray1)
32-
console.log(strArray2)
33+
let strArrayForTempVariable = [...strArray1]
34+
console.time("reverseStringUsingTempVariable")
35+
reverseStringUsingTempVariable(strArrayForTempVariable)
36+
console.timeEnd("reverseStringUsingTempVariable")
37+
38+
console.log(strArrayForDestructuring)
39+
console.log(strArrayForTempVariable)

0344-Reverse_String/readme.md

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
Write a function that reverses a string. The input string is given as an array of characters s.
1+
Write a function that reverses a string. The input string is given as an array of characters `s`.
22

3-
You must do this by modifying the input array [in-place](https://en.wikipedia.org/wiki/In-place_algorithm) with O(1) extra memory.
3+
You must do this by modifying the input array [in-place](https://en.wikipedia.org/wiki/In-place_algorithm) with `O(1)` extra memory.
44

55
Example 1:
66

7-
> Input: s = ["h","e","l","l","o"]<br>
8-
> Output: ["o","l","l","e","h"]
7+
> <span style="color: white;">Input</span>: s = ["h","e","l","l","o"]<br>
8+
> <span style="color: white;">Output</span>: ["o","l","l","e","h"]
99
1010
Example 2:
1111

12-
> Input: s = ["H","a","n","n","a","h"]<br>
13-
> Output: ["h","a","n","n","a","H"]
12+
> <span style="color: white;">Input</span>: s = ["H","a","n","n","a","h"]<br>
13+
> <span style="color: white;">Output</span>: ["h","a","n","n","a","H"]
1414
1515
Constraints:
1616

17-
- 1 <= s.length <= 105
18-
- s[i] is a [printable ascii character](https://en.wikipedia.org/wiki/ASCII#Printable_characters).
17+
- 1 <= s.length <= 10<sup>5</sup>
18+
- `s[i]` is a [printable ascii character](https://en.wikipedia.org/wiki/ASCII#Printable_characters).

0 commit comments

Comments
 (0)