-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdetermine_if_string_halves_are_alike.dart
145 lines (114 loc) · 3.13 KB
/
determine_if_string_halves_are_alike.dart
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
-* 1704. Determine if String Halves Are Alike *-
You are given a string s of even length. Split this string into two halves of equal lengths, and let a be the first half and b be the second half.
Two strings are alike if they have the same number of vowels ('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'). Notice that s contains uppercase and lowercase letters.
Return true if a and b are alike. Otherwise, return false.
Example 1:
Input: s = "book"
Output: true
Explanation: a = "bo" and b = "ok". a has 1 vowel and b has 1 vowel. Therefore, they are alike.
Example 2:
Input: s = "textbook"
Output: false
Explanation: a = "text" and b = "book". a has 1 vowel whereas b has 2. Therefore, they are not alike.
Notice that the vowel o is counted twice.
Constraints:
2 <= s.length <= 1000
s.length is even.
s consists of uppercase and lowercase letters.
*/
import 'dart:collection';
class A {
bool halvesAreAlike(String s) {
// set of Vowels
HashSet<String> vowels =
HashSet.of(['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']);
// just to count each and every value of occurrence
int count = 0;
//loop for each element in the string
for (int i = 0; i < s.length; i++) {
// if they both match
if (vowels.contains(s[i])) {
// we will set the value to count
count += (s.length - 2 * i - 1).sign;
}
}
// than return based on the matching
return count == 0;
}
}
class B {
bool halvesAreAlike(String s) {
int firstCount = 0;
int secondCount = 0;
for (int i = 0; i < s.length ~/ 2; i++) {
if (s[i] == 'a' ||
s[i] == 'e' ||
s[i] == 'i' ||
s[i] == 'o' ||
s[i] == 'u' ||
s[i] == 'A' ||
s[i] == 'E' ||
s[i] == 'I' ||
s[i] == 'O' ||
s[i] == 'U') {
firstCount++;
}
}
for (int i = s.length ~/ 2; i < s.length; i++) {
if (s[i] == 'a' ||
s[i] == 'e' ||
s[i] == 'i' ||
s[i] == 'o' ||
s[i] == 'u' ||
s[i] == 'A' ||
s[i] == 'E' ||
s[i] == 'I' ||
s[i] == 'O' ||
s[i] == 'U') {
secondCount++;
}
}
if (firstCount == secondCount) return true;
return false;
}
}
class C {
bool halvesAreAlike(String s) {
List<String> ch = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'];
int vow1 = 0;
int vow2 = 0;
for (int i = 0; i < s.length / 2; i++) {
for (int k = 0; k < ch.length; k++) {
if (s[i] == ch[k]) {
vow1++;
}
}
}
for (int i = s.length ~/ 2; i < s.length; i++) {
for (int k = 0; k < ch.length; k++) {
if (s[i] == ch[k]) {
vow2++;
}
}
}
if (vow1 == vow2) {
return true;
}
return false;
}
}
class D {
bool halvesAreAlike(String s) {
String st = "";
st = s.toLowerCase();
List<String> c = st.split("");
int count = 0;
for (int i = 0; i < c.length; i++) {
if (i == 'a' || i == 'e' || i == 'i' || i == 'o' || i == 'u') {
count++;
}
}
return count % 2 == 0 ? true : false;
}
}