Solution: Determine if String Halves Are Alike
Tags: algorithms, javascript, java, python
Published: 2021-04-07 08:36:37

*This is part of a series of Leetcode solution explanations ([index](https://dev.to/seanpgallivan/leetcode-solutions-index-57fl)). If you liked this solution or found it useful,* ***please like*** *this post and/or* ***upvote*** *[my solution post on Leetcode's forums](https://leetcode.com/problems/determine-if-string-halves-are-alike/discuss/1146651).*

---

#### [Leetcode Problem #1704 (*Easy*): Determine if String Halves Are Alike](https://leetcode.com/problems/determine-if-string-halves-are-alike/)

---

#### ***Description:***

<br />(*Jump to*: [*Solution Idea*](#idea) || *Code*: [*JavaScript*](#javascript-code) | [*Python*](#python-code) | [*Java*](#java-code) | [*C++*](#c-code))

>
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`.

---

#### ***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.<br>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:***

<br />(*Jump to*: [*Problem Description*](#description) || *Code*: [*JavaScript*](#javascript-code) | [*Python*](#python-code) | [*Java*](#java-code) | [*C++*](#c-code))

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:***
<br />(*Jump to*: [*Problem Description*](#description) || [*Solution Idea*](#idea))
```javascript
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:***
<br />(*Jump to*: [*Problem Description*](#description) || [*Solution Idea*](#idea))
```python
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:***
<br />(*Jump to*: [*Problem Description*](#description) || [*Solution Idea*](#idea))
```java
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:***
<br />(*Jump to*: [*Problem Description*](#description) || [*Solution Idea*](#idea))
```c++
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;
    }
};
```