Leetcode 1704. Determine if String Halves Are Alike
Problem Description
You are given a string s
of even length. Your task is to split this string into two halves of equal lengths, let a
be the first half and b
be the second half. Two strings are considered 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. Your goal is to return true if a
and b
are alike, otherwise, return false.
Examples
-
Input: s = "book" Output: true Explanation:ย a = "bo" and b = "ok". a has 1 vowel and b has 1 vowel. Therefore, they are alike.
-
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.
-
Input: s = "MerryChristmas" Output: false
-
Input: s = "AbCdEfGh" Output: true
Constraints
- 2 <= s.length <= 1000
- s.length is even.
- s consists of uppercase and lowercase letters.
Approach
To solve this problem, we can iterate through both halves of the string and count the number of vowels in each half. Finally, we can compare the number of vowels in both halves to determine if they are alike or not.
Algorithm
- Create a function
is_vowel
to check if a character is a vowel. - Split the input string
s
into two equal halvesa
andb
. - Initialize two counters,
vowel_count_a
andvowel_count_b
, to 0. - Iterate through both halves of the string.
a. For each character in
a
, if it's a vowel, incrementvowel_count_a
. b. For each character inb
, if it's a vowel, incrementvowel_count_b
. - If
vowel_count_a
is equal tovowel_count_b
, return true; otherwise, return false.
Python Solution
1class Solution:
2 def is_vowel(self, char):
3 return char in 'aeiouAEIOU'
4
5 def halves_are_alike(self, s: str) -> bool:
6 a = s[:len(s)//2]
7 b = s[len(s)//2:]
8
9 vowel_count_a = sum(1 for char in a if self.is_vowel(char))
10 vowel_count_b = sum(1 for char in b if self.is_vowel(char))
11
12 return vowel_count_a == vowel_count_b
Java Solution
1class Solution {
2 public boolean isVowel(char c) {
3 return "aeiouAEIOU".indexOf(c) != -1;
4 }
5
6 public boolean halvesAreAlike(String s) {
7 int n = s.length();
8 String a = s.substring(0, n / 2);
9 String b = s.substring(n / 2);
10
11 int vowelCountA = 0, vowelCountB = 0;
12 for (int i = 0; i < a.length(); i++) {
13 if (isVowel(a.charAt(i))) vowelCountA++;
14 if (isVowel(b.charAt(i))) vowelCountB++;
15 }
16
17 return vowelCountA == vowelCountB;
18 }
19}
JavaScript Solution
1class Solution {
2 isVowel(char) {
3 return 'aeiouAEIOU'.includes(char);
4 }
5
6 halvesAreAlike(s) {
7 const n = s.length;
8 const a = s.slice(0, n / 2);
9 const b = s.slice(n / 2);
10
11 let vowelCountA = 0, vowelCountB = 0;
12 for (let i = 0; i < a.length; i++) {
13 if (this.isVowel(a[i])) vowelCountA++;
14 if (this.isVowel(b[i])) vowelCountB++;
15 }
16
17 return vowelCountA === vowelCountB;
18 }
19}
C++ Solution
1#include <string>
2
3class Solution {
4public:
5 bool isVowel(char c) {
6 return std::string("aeiouAEIOU").find(c) != std::string::npos;
7 }
8
9 bool halvesAreAlike(std::string s) {
10 int n = s.size();
11 std::string a = s.substr(0, n / 2);
12 std::string b = s.substr(n / 2);
13
14 int vowelCountA = 0, vowelCountB = 0;
15 for (int i = 0; i < a.size(); i++) {
16 if (isVowel(a[i])) vowelCountA++;
17 if (isVowel(b[i])) vowelCountB++;
18 }
19
20 return vowelCount_a == vowelCount_b;
21 }
22};
C# Solution
1using System;
2
3public class Solution {
4 public bool IsVowel(char c) {
5 return "aeiouAEIOU".IndexOf(c) != -1;
6 }
7
8 public bool HalvesAreAlike(string s) {
9 int n = s.Length;
10 string a = s.Substring(0, n / 2);
11 string b = s.Substring(n / 2);
12
13 int vowelCountA = 0, vowelCountB = 0;
14 for (int i = 0; i < a.Length; i++) {
15 if (IsVowel(a[i])) vowelCountA++;
16 if (IsVowel(b[i])) vowelCountB++;
17 }
18
19 return vowelCountA == vowelCountB;
20 }
21}
In summary, the problem presented was to determine if two halves of a given string are alike in terms of vowel counts. To solve this, we wrote a simple algorithm that splits the input string, and counts the vowels in each half. If the vowel count is the same for both halves, then the function returns true; otherwise, false. We also provided solutions in Python, Java, JavaScript, C++, and C#.
Got a question?ย Ask the Teaching Assistantย anything you don't understand.
Still not clear? Ask in the Forum, ย Discordย orย Submitย the part you don't understand to our editors.