This is part of a series of Leetcode solution explanations (index). If you liked this solution or found it useful, please like this post and/or upvote my solution post on Leetcode's forums.
Leetcode Problem #1704 (Easy): Determine if String Halves Are Alike
Description:
(Jump to: Solution Idea || Code: JavaScript | Python | Java | C++)
You are given a string
s
of even length. Split this string into two halves of equal lengths, and leta
be the first half andb
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 thats
contains uppercase and lowercase letters.Return
true
ifa
andb
are alike. Otherwise, returnfalse
.
Examples:
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.
Idea:
(Jump to: Problem Description || Code: JavaScript | Python | Java | C++)
This problem is pretty straightforward. The first issue is being able to identify vowels. There are obviously many ways to do this, but this seems like a good place to use some kind of vowel lookup data structure (vowels). Depending on the language, it can be a string, a dictionary, a map, a set, etc.
Then we just need to keep a balance counter (ans) and iterate through both halves of the input string (S) and increment ans whenever the first half has a vowel and decrement ans whenever the second half has a vowel.
Once we're done, we can just return ans == 0 to determine if the two string halves are balanced.
Javascript Code:
(Jump to: Problem Description || Solution Idea)
const vowels = "aeiouAEIOU"
var halvesAreAlike = function(S) {
let mid = S.length / 2, ans = 0
for (let i = 0, j = mid; i < mid; i++, j++)
ans += vowels.includes(S.charAt(i)) - vowels.includes(S.charAt(j))
return ans === 0
};
Python Code:
(Jump to: Problem Description || Solution Idea)
vowels = "aeiouAEIOU"
class Solution:
def halvesAreAlike(self, S: str) -> bool:
mid, ans = len(S) // 2, 0
for i in range(mid):
if S[i] in vowels: ans += 1
if S[mid+i] in vowels: ans -=1
return ans == 0
Java Code:
(Jump to: Problem Description || Solution Idea)
class Solution {
String vowels = "aeiouAEIOU";
public boolean halvesAreAlike(String S) {
int mid = S.length() / 2, ans = 0;
for (int i = 0, j = mid; i < mid; i++, j++) {
if (vowels.indexOf(S.charAt(i)) >= 0) ans++;
if (vowels.indexOf(S.charAt(j)) >= 0) ans--;
}
return ans == 0;
}
}
C++ Code:
(Jump to: Problem Description || Solution Idea)
string vowels = "aeiouAEIOU";
class Solution {
public:
bool halvesAreAlike(string S) {
int mid = S.size() / 2, ans = 0;
for (int i = 0, j = mid; i < mid; i++, j++) {
if (~vowels.find(S[i])) ans++;
if (~vowels.find(S[j])) ans--;
}
return ans == 0;
}
};
Top comments (2)
I think using Map or Object will reduce time to check.
'aeiouAEIOU'.includes
always O(n) but using Map it is only O(1)great. thanks for sharing.