Skip to content

Commit 0bcd899

Browse files
authored
0344: Reverse String (#1)
1 parent 56d14cd commit 0bcd899

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

0344-Reverse_String/main.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
let strArray1 = ["h", "e", "l", "l", "o"];
2+
let strArray2 = ["H", "a", "n", "n", "a", "h"];
3+
function reverseStringUsingDestructuring(s) {
4+
let left = 0;
5+
let right = s.length - 1;
6+
while (left < right) {
7+
;
8+
[s[left], s[right]] = [s[right], s[left]];
9+
left++;
10+
right--;
11+
}
12+
}
13+
function reverseStringUsingTempVariable(s) {
14+
let length = s.length;
15+
let left = 0, right = length - 1;
16+
let tempStr;
17+
while (left < right) {
18+
tempStr = s[left];
19+
s[left] = s[right];
20+
s[right] = tempStr;
21+
left++;
22+
right--;
23+
}
24+
}
25+
reverseStringUsingDestructuring(strArray1);
26+
reverseStringUsingTempVariable(strArray2);
27+
console.log(strArray1);
28+
console.log(strArray2);

0344-Reverse_String/main.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
let strArray1 = ["h", "e", "l", "l", "o"]
2+
let strArray2 = ["H", "a", "n", "n", "a", "h"]
3+
4+
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+
}
12+
}
13+
14+
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+
}
26+
}
27+
28+
reverseStringUsingDestructuring(strArray1)
29+
reverseStringUsingTempVariable(strArray2)
30+
31+
console.log(strArray1)
32+
console.log(strArray2)

0344-Reverse_String/readme.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Write a function that reverses a string. The input string is given as an array of characters s.
2+
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.
4+
5+
Example 1:
6+
7+
> Input: s = ["h","e","l","l","o"]<br>
8+
> Output: ["o","l","l","e","h"]
9+
10+
Example 2:
11+
12+
> Input: s = ["H","a","n","n","a","h"]<br>
13+
> Output: ["h","a","n","n","a","H"]
14+
15+
Constraints:
16+
17+
- 1 <= s.length <= 105
18+
- s[i] is a [printable ascii character](https://en.wikipedia.org/wiki/ASCII#Printable_characters).

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy