Skip to content

Commit 4452982

Browse files
committed
leetcode
1 parent 31f788b commit 4452982

File tree

4 files changed

+286
-0
lines changed

4 files changed

+286
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
123123
- [**303.** Range Sum Query - Immutable](RangeSumQuery-Immutable/range_sum_query_immutable.dart)
124124
- [**433.** Minimum Genetic Mutation](MinimumGeneticMutation/minimum_genetic_mutation.dart)
125125
- [**2131.** Longest Palindrome by Concatenating Two Letter Words](LongestPalindromeByConcatenatingTwoLetterWords/longest_palindrome_by_concatenating_two_letter_words.dart)
126+
- [**345.** Reverse Vowels of a String](ReverseVowelsOfAString/reverse_vowels_of_a_string.dart)
126127

127128
## Reach me via
128129

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
/*
2+
3+
4+
-* 345. Reverse Vowels of a String *-
5+
6+
Given a string s, reverse only all the vowels in the string and return it.
7+
8+
The vowels are 'a', 'e', 'i', 'o', and 'u', and they can appear in both lower and upper cases, more than once.
9+
10+
11+
12+
Example 1:
13+
14+
Input: s = "hello"
15+
Output: "holle"
16+
Example 2:
17+
18+
Input: s = "leetcode"
19+
Output: "leotcede"
20+
21+
22+
Constraints:
23+
24+
1 <= s.length <= 3 * 105
25+
s consist of printable ASCII characters.
26+
Accepted
27+
403,544
28+
Submissions
29+
838,146
30+
31+
32+
*/
33+
34+
class A {
35+
String reverseVowels(String s) {
36+
List<String> char = s.split("");
37+
int left = 0;
38+
int right = s.length - 1;
39+
while (left < right) {
40+
while (left < right && !isVowels(char[left])) {
41+
left++;
42+
}
43+
while (left < right && !isVowels(char[right])) {
44+
right--;
45+
}
46+
if (left < right) {
47+
String temp = char[left];
48+
char[left++] = char[right];
49+
char[right--] = temp;
50+
}
51+
}
52+
return char.join();
53+
}
54+
55+
bool isVowels(String s) {
56+
return s == 'a' ||
57+
s == 'e' ||
58+
s == 'i' ||
59+
s == 'o' ||
60+
s == 'u' ||
61+
s == 'A' ||
62+
s == 'E' ||
63+
s == 'I' ||
64+
s == 'O' ||
65+
s == 'U';
66+
}
67+
}
68+
69+
class B {
70+
// Set<String> vowels = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'};
71+
String vowels = 'aeiouAEIOU';
72+
String reverseVowels(String s) {
73+
List<String> char = s.split("");
74+
for (int i = 0, j = char.length - 1; i < j; i++, j--) {
75+
while (!vowels.contains(char[i]) && i < j) {
76+
i++;
77+
}
78+
79+
while (!vowels.contains(char[j]) && i < j) {
80+
j--;
81+
}
82+
83+
// [char[i], char[j]] = [char[j], char[i]];
84+
85+
char[i] = char[j];
86+
char[j] = char[i];
87+
}
88+
89+
return char.join('');
90+
}
91+
}
92+
93+
class C {
94+
String reverseVowels(String s) {
95+
String str = "aeiouAEIOU";
96+
String dummy = "";
97+
for (String c in s.split("")) {
98+
if (str.contains("" + c)) dummy += c;
99+
}
100+
List<String> c = s.split("");
101+
int j = dummy.length - 1;
102+
for (int i = 0; i < s.length; i++) {
103+
if (str.contains("" + s[i])) {
104+
c[i] = dummy[j];
105+
j--;
106+
}
107+
}
108+
109+
return c.join();
110+
}
111+
}
112+
113+
class D {
114+
String reverseVowels(String s) {
115+
// Create A Pointer Which Is Start From End Of The Array.
116+
int pointer = s.length - 1;
117+
// Convert String To Array That Will More Easy To Solve This Problem.
118+
List<String> ansStr = s.split("");
119+
int i = 0; // Create A I Veritable That Start From Array Index Of 0
120+
// Create a All Vowels That Will Help To Check The Value Is Vowel Or Not.
121+
List<String> vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'];
122+
// You can use regex like: /[a|e|i|o|u]/gi
123+
while (i < pointer) {
124+
// Check Condition That I Should Be Less Then Pointer This Condition Divide The Time Complexity To O(N) To O(LogN).
125+
126+
if (vowels.contains(ansStr[i]) && vowels.contains(ansStr[pointer])) {
127+
// Here we check the condition that our I position value and Pointer position value should be vowel
128+
String tmp = ansStr[i];
129+
// If condition is true then swap this position value respectively you can create a extra swap function.
130+
ansStr[i++] = ansStr[pointer];
131+
ansStr[pointer--] = tmp;
132+
} else {
133+
if (vowels.contains(ansStr[i])) {
134+
// If the Ith position value is vowel then we decrement the Pointer Position value to -1;
135+
pointer--;
136+
} else {
137+
i++; // If the Ith position value does not a vowel then increment the Ith position by +1
138+
}
139+
}
140+
}
141+
return ansStr.join(""); // Here we convert the array to string
142+
}
143+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package main
2+
3+
import "unicode"
4+
5+
// Time Complexity: O(N)
6+
// Space Complexity: O(N)
7+
8+
// fun fact:
9+
// `Y` and `y` can be a vowel as well sometimes.
10+
// glad the problem statement defines it well
11+
func isVowel(c rune) bool {
12+
// alternatively, we can just check
13+
// return c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U' ||
14+
// c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'
15+
c = unicode.ToLower(c)
16+
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'
17+
}
18+
19+
func reverseVowels(s string) string {
20+
ss := []rune(s)
21+
n := len(ss)
22+
l, r := 0, n-1
23+
for l < r {
24+
// find the index of the first vowel in the given range
25+
for l < r && !isVowel(ss[l]) {
26+
l += 1
27+
}
28+
// find the index of last vowel in the given range
29+
for r > l && !isVowel(ss[r]) {
30+
r -= 1
31+
}
32+
// swap ss[l] and ss[r]
33+
ss[l], ss[r] = ss[r], ss[l]
34+
// since we've processed the character ss[l],
35+
// we move the left pointer to the right
36+
l += 1
37+
// since we've processed the character ss[r],
38+
// we move the right pointer to the left
39+
r -= 1
40+
}
41+
return string(ss)
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# 🔥 Reverse Vowels of a String 🔥 || 3 Approaches || Simple Fast and Easy || with Explanation
2+
3+
## Solution -1
4+
5+
```dart
6+
class Solution {
7+
String reverseVowels(String s) {
8+
List<String> char = s.split("");
9+
int left = 0;
10+
int right = s.length - 1;
11+
while (left < right) {
12+
while (left < right && !isVowels(char[left])) {
13+
left++;
14+
}
15+
while (left < right && !isVowels(char[right])) {
16+
right--;
17+
}
18+
if (left < right) {
19+
String temp = char[left];
20+
char[left++] = char[right];
21+
char[right--] = temp;
22+
}
23+
}
24+
return char.join();
25+
}
26+
27+
bool isVowels(String s) {
28+
return s == 'a' ||
29+
s == 'e' ||
30+
s == 'i' ||
31+
s == 'o' ||
32+
s == 'u' ||
33+
s == 'A' ||
34+
s == 'E' ||
35+
s == 'I' ||
36+
s == 'O' ||
37+
s == 'U';
38+
}
39+
}
40+
```
41+
42+
## Solution - 2
43+
44+
```dart
45+
class Solution {
46+
String reverseVowels(String s) {
47+
String str = "aeiouAEIOU";
48+
String dummy = "";
49+
for (String c in s.split("")) {
50+
if (str.contains("" + c)) dummy += c;
51+
}
52+
List<String> c = s.split("");
53+
int j = dummy.length - 1;
54+
for (int i = 0; i < s.length; i++) {
55+
if (str.contains("" + s[i])) {
56+
c[i] = dummy[j];
57+
j--;
58+
}
59+
}
60+
61+
return c.join();
62+
}
63+
}
64+
```
65+
66+
## Solution - 3
67+
68+
```dart
69+
class Solution {
70+
String reverseVowels(String s) {
71+
// Create A Pointer Which Is Start From End Of The Array.
72+
int pointer = s.length - 1;
73+
// Convert String To Array That Will More Easy To Solve This Problem.
74+
List<String> ansStr = s.split("");
75+
int i = 0; // Create A I Veritable That Start From Array Index Of 0
76+
// Create a All Vowels That Will Help To Check The Value Is Vowel Or Not.
77+
List<String> vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'];
78+
// You can use regex like: /[a|e|i|o|u]/gi
79+
while (i < pointer) {
80+
// Check Condition That I Should Be Less Then Pointer This Condition Divide The Time Complexity To O(N) To O(LogN).
81+
82+
if (vowels.contains(ansStr[i]) && vowels.contains(ansStr[pointer])) {
83+
// Here we check the condition that our I position value and Pointer position value should be vowel
84+
String tmp = ansStr[i];
85+
// If condition is true then swap this position value respectively you can create a extra swap function.
86+
ansStr[i++] = ansStr[pointer];
87+
ansStr[pointer--] = tmp;
88+
} else {
89+
if (vowels.contains(ansStr[i])) {
90+
// If the Ith position value is vowel then we decrement the Pointer Position value to -1;
91+
pointer--;
92+
} else {
93+
i++; // If the Ith position value does not a vowel then increment the Ith position by +1
94+
}
95+
}
96+
}
97+
return ansStr.join(""); // Here we convert the array to string
98+
}
99+
}
100+
```

0 commit comments

Comments
 (0)