Skip to content

Commit 5f9211e

Browse files
committed
leetcode
1 parent 948cd7f commit 5f9211e

File tree

4 files changed

+176
-0
lines changed

4 files changed

+176
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
131131
- [**338.** Counting Bits](CountingBits/counting_bits.dart)
132132
- [**374.** Guess Number Higher or Lower](GuessNumberHigherOrLower/guess_number_higher_or_lower.dart)
133133
- [**342.** Power of Four](PowerOfFour/power_of_four.dart)
134+
- [**344.** Reverse String](ReverseString/reverse_string.dart)
134135

135136
## Reach me via
136137

ReverseString/reverse_string.dart

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
3+
4+
5+
-* 344. Reverse String *-
6+
7+
8+
Write a function that reverses a string. The input string is given as an array of characters s.
9+
10+
You must do this by modifying the input array in-place with O(1) extra memory.
11+
12+
13+
14+
Example 1:
15+
16+
Input: s = ["h","e","l","l","o"]
17+
Output: ["o","l","l","e","h"]
18+
Example 2:
19+
20+
Input: s = ["H","a","n","n","a","h"]
21+
Output: ["h","a","n","n","a","H"]
22+
23+
24+
Constraints:
25+
26+
1 <= s.length <= 105
27+
s[i] is a printable ascii character.
28+
29+
*/
30+
31+
class A {
32+
void solve(List<String> s, int left, int right) {
33+
if (left >= right) return;
34+
String temp = s[left];
35+
s[left] = s[right];
36+
s[right] = temp;
37+
solve(s, ++left, --right);
38+
}
39+
40+
void reverseString(List<String> s) {
41+
int left = 0;
42+
int right = s.length - 1;
43+
solve(s, left, right);
44+
}
45+
}
46+
47+
class B {
48+
void reverseString(List<String> s) {
49+
int left = 0, right = s.length - 1;
50+
while (left < right) {
51+
String temp = s[right];
52+
s[right--] = s[left];
53+
s[left++] = temp;
54+
}
55+
}
56+
}
57+
58+
class C {
59+
void reverseString(List<String> s) {
60+
// Declare integer variable and initialize them
61+
int i = 0, j = 0, k = 0;
62+
// Initialize stack
63+
List<String> stack = [];
64+
// Initialize string
65+
String str = s.toString();
66+
// Push all elements into the stack
67+
for (i = 0; i < str.length; i++) {
68+
stack.add(s[i]);
69+
}
70+
// Initialize another array
71+
List<String> array = List.filled(s.length, "");
72+
// Pop elements from stack
73+
while (stack.length > 0) {
74+
s[j++] = stack.removeLast(); //Increment j accordingly
75+
}
76+
// Fill the other array with popped elements from stack
77+
for (k = 0; k < str.length; k++) {
78+
array[k] = str[k];
79+
}
80+
}
81+
}
82+
83+
class D {
84+
void reverseString(List<String> s) {
85+
//create a stack
86+
List<String> st = [];
87+
//loop through the string, to add all the string elements in stack
88+
for (int i = 0; i < s.length; i++) {
89+
//create a variable to add the values
90+
String ch = s[i];
91+
st.add(ch);
92+
}
93+
//empty the original string
94+
s.clear();
95+
//add the stack elements to the string
96+
while (!st.isEmpty) {
97+
String ch = st.first;
98+
//reversed
99+
s.add(ch);
100+
st.removeLast();
101+
}
102+
}
103+
}

ReverseString/reverse_string.go

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package main
2+
3+
func reverseString(s []byte) {
4+
l, r := 0, len(s)-1
5+
for i := 0; i < len(s)/2; i++ {
6+
l = i
7+
r = len(s) - (i + 1)
8+
s[l], s[r] = s[r], s[l]
9+
}
10+
}

ReverseString/reverse_string.md

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# 🔥 Reverse String 🔥 || 3 Approaches || Simple Fast and Easy || with Explanation
2+
3+
## Solution - 1
4+
5+
```dart
6+
class Solution {
7+
void solve(List<String> s, int left, int right) {
8+
if (left >= right) return;
9+
String temp = s[left];
10+
s[left] = s[right];
11+
s[right] = temp;
12+
solve(s, ++left, --right);
13+
}
14+
15+
void reverseString(List<String> s) {
16+
int left = 0;
17+
int right = s.length - 1;
18+
solve(s, left, right);
19+
}
20+
}
21+
```
22+
23+
## Solution - 2
24+
25+
```dart
26+
class Solution {
27+
void reverseString(List<String> s) {
28+
int left = 0, right = s.length - 1;
29+
while (left < right) {
30+
String temp = s[right];
31+
s[right--] = s[left];
32+
s[left++] = temp;
33+
}
34+
}
35+
}
36+
```
37+
38+
## Solution - 3
39+
40+
```dart
41+
class Solution {
42+
void reverseString(List<String> s) {
43+
//create a stack
44+
List<String> st = [];
45+
//loop through the string, to add all the string elements in stack
46+
for (int i = 0; i < s.length; i++) {
47+
//create a variable to add the values
48+
String ch = s[i];
49+
st.add(ch);
50+
}
51+
//empty the original string
52+
s.clear();
53+
//add the stack elements to the string
54+
while (!st.isEmpty) {
55+
String ch = st.first;
56+
//reversed
57+
s.add(ch);
58+
st.removeLast();
59+
}
60+
}
61+
}
62+
```

0 commit comments

Comments
 (0)