##### Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/1704.html

# 1704. Determine if String Halves Are Alike (Easy)

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.


Example 3:

Input: s = "MerryChristmas"
Output: false


Example 4:

Input: s = "AbCdEfGh"
Output: true


Constraints:

• 2 <= s.length <= 1000
• s.length is even.
• s consists of uppercase and lowercase letters.

Related Topics:
String

## Solution 1.

• class Solution {
public boolean halvesAreAlike(String s) {
s = s.toLowerCase();
int length = s.length();
int halfLength = length / 2;
int vowels1 = 0, vowels2 = 0;
for (int i = 0; i < halfLength; i++) {
char c1 = s.charAt(i), c2 = s.charAt(i + halfLength);
if (isVowel(c1))
vowels1++;
if (isVowel(c2))
vowels2++;
}
return vowels1 == vowels2;
}

public boolean isVowel(char c) {
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
}
}

############

class Solution {
private static final Set<Character> VOWELS
= Set.of('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U');

public boolean halvesAreAlike(String s) {
int cnt = 0, n = s.length() >> 1;
for (int i = 0; i < n; ++i) {
cnt += VOWELS.contains(s.charAt(i)) ? 1 : 0;
cnt -= VOWELS.contains(s.charAt(i + n)) ? 1 : 0;
}
return cnt == 0;
}
}

• // OJ: https://leetcode.com/problems/determine-if-string-halves-are-alike/
// Time: O(N)
// Space: O(N)
class Solution {
int count(string s) {
int cnt = 0;
for (char c : s) {
c = tolower(c);
cnt += c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
}
return cnt;
}
public:
bool halvesAreAlike(string s) {
return count(s.substr(0, s.size() / 2)) == count(s.substr(s.size() / 2));
}
};

• class Solution:
def halvesAreAlike(self, s: str) -> bool:
cnt, n = 0, len(s) >> 1
vowels = set('aeiouAEIOU')
for i in range(n):
cnt += s[i] in vowels
cnt -= s[i + n] in vowels
return cnt == 0

############

# 1704. Determine if String Halves Are Alike
# https://leetcode.com/problems/determine-if-string-halves-are-alike/

class Solution:
def halvesAreAlike(self, s: str) -> bool:
half = len(s) // 2

def count(s):
return sum(c in "aeiouAEIOU" for c in s)

return count(s[:half]) == count(s[half:])


• func halvesAreAlike(s string) bool {
vowels := map[byte]bool{}
for _, c := range "aeiouAEIOU" {
vowels[byte(c)] = true
}
cnt, n := 0, len(s)>>1
for i := 0; i < n; i++ {
if vowels[s[i]] {
cnt++
}
if vowels[s[i+n]] {
cnt--
}
}
return cnt == 0
}

• function halvesAreAlike(s: string): boolean {
const set = new Set(['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']);
const n = s.length >> 1;
let count = 0;
for (let i = 0; i < n; i++) {
set.has(s[i]) && count++;
set.has(s[n + i]) && count--;
}
return count === 0;
}


• use std::collections::HashSet;
impl Solution {
pub fn halves_are_alike(s: String) -> bool {
let set: HashSet<&u8> = [b'a', b'e', b'i', b'o', b'u', b'A', b'E', b'I', b'O', b'U']
.into_iter()
.collect();
let s = s.as_bytes();
let n = s.len() >> 1;
let mut count = 0;
for i in 0..n {
if set.contains(&s[i]) {
count += 1;
}
if set.contains(&s[n + i]) {
count -= 1;
}
}
count == 0
}
}


• /**
* @param {string} s
* @return {boolean}
*/
var halvesAreAlike = function (s) {
const str = 'aeiouAEIOU';
let cnt = 0;
for (let i = 0; i < s.length / 2; i++) {
if (str.indexOf(s[i]) > -1) cnt++;
if (str.indexOf(s[s.length - 1 - i]) > -1) cnt--;
}
return cnt === 0;
};


• class Solution {
/**
* @param String $s * @return Boolean */ function halvesAreAlike($s) {
$cnt = 0; for ($i = 0; $i < strlen($s) / 2; $i++) { if (strpos("aeiouAEIOU",$s[$i]) !== false)$cnt++;
if (strpos("aeiouAEIOU", $s[strlen($s) / 2 + $i]) !== false)$cnt--;
}
return \$cnt == 0;
}
}