Skip to content

Commit aede017

Browse files
Easy
1 parent 53cd4f1 commit aede017

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

If_str_half_alike.cpp

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// 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.
2+
// 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.
3+
// Return true if a and b are alike. Otherwise, return false.
4+
5+
// Example 1:
6+
7+
// Input: s = "book"
8+
// Output: true
9+
// Explanation: a = "bo" and b = "ok". a has 1 vowel and b has 1 vowel. Therefore, they are alike.
10+
// Example 2:
11+
12+
// Input: s = "textbook"
13+
// Output: false
14+
// Explanation: a = "text" and b = "book". a has 1 vowel whereas b has 2. Therefore, they are not alike.
15+
// Notice that the vowel o is counted twice.
16+
17+
18+
/*
19+
class Solution {
20+
public:
21+
bool halvesAreAlike(string s) {
22+
int len=s.size();
23+
int mid=len/2;
24+
25+
int count_first=0;
26+
int count_next=0;
27+
28+
// 0 1 2 3 4 5
29+
// 0 1 2 3 4 5
30+
// 0 1 2 3+0 3+1 3+2
31+
32+
for(int i=0; i<mid; i++){
33+
if(isVowel(s[i])){
34+
count_first++;
35+
}
36+
37+
if(isVowel(s[mid+i])){
38+
count_next++;
39+
}
40+
}
41+
return count_first == count_next;
42+
}
43+
44+
// function to check each character, if vowel
45+
46+
bool isVowel(char c){
47+
return c=='a'|| c=='e'|| c=='i'|| c=='o'|| c=='u'|| c=='A'|| c=='E'|| c=='I'|| c=='O'|| c=='U' ;
48+
}
49+
};
50+
*/

0 commit comments

Comments
 (0)