Determine if String Halves Are Alike

Easy
237.

Determine if String Halves Are Alike

Determine if String Halves Are Alike is an easy coding problem on strings and counting, asked in Amazon and Adobe interviews.

You are given a string s of even length. Split it into two halves of equal length.

Two strings are alike if they contain the same number of vowels (a, e, i, o, u, in either case). Return true if the two halves are alike.

Example 1

Input
s = "book"
Output
true
Explanation
"bo" and "ok" each contain one vowel.

Example 2

Input
s = "textbook"
Output
false
Explanation
"text" has one vowel and "book" has two.

Example 3

Input
s = "AbCdEfGh"
Output
true

Constraints

  • 2 <= s.length <= 40
  • s.length is even.
  • s consists of upper and lowercase English letters.
Write your solution here. Running it against the tests needs a free account — what you type comes with you.Sign in to run
/**
 * @param {string} s
 * @return {boolean}
 */
var halvesAreAlike = function(s) {
    // Write your code here
    
};

Sign in to run your own test cases

Sign in

Determine if String Halves Are Alike — Problem & Solution

Determine if String Halves Are Alike solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem.

Determine if String Halves Are Alike is an easy coding problem on strings and counting, asked in Amazon and Adobe interviews.

  • Difficulty: Easy
  • Topics: Strings, Counting
  • Asked at: Amazon, Adobe
  • Time limit: 2 s
  • Memory limit: 256 MB
  • Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby

Problem statement

You are given a string s of even length. Split it into two halves of equal length.

Two strings are alike if they contain the same number of vowels (a, e, i, o, u, in either case). Return true if the two halves are alike.

Example 1

Input: s = "book"
Output: true
Explanation: "bo" and "ok" each contain one vowel.

Example 2

Input: s = "textbook"
Output: false
Explanation: "text" has one vowel and "book" has two.

Example 3

Input: s = "AbCdEfGh"
Output: true

Constraints

  • 2 <= s.length <= 40
  • s.length is even.
  • s consists of upper and lowercase English letters.

Hints for Determine if String Halves Are Alike

  1. Hint 1

    Count vowels in the first half and in the second half, then compare.

  2. Hint 2

    Both cases count: treat A exactly like a.

  3. Hint 3

    One pass suffices — pick which counter to bump from the index.

How to solve Determine if String Halves Are Alike

Count vowels in each half and compare.

Approach

  1. Let half = n / 2.
  2. Count vowels (both cases) in s[:half] and in s[half:].
  3. Return whether the counts are equal.

Why it works

"Alike" is defined purely by vowel counts; the halves are fixed by the even length.

Complexity

  • Time — O(n)
  • Space — O(1)

Pitfalls

  • Include uppercase vowels in the set.
  • A single pass with +1 for the first half and -1 for the second, checking for zero, avoids two counters.

Reference solution

Python

def halvesAreAlike(s: str) -> bool:
    vowels = set("aeiouAEIOU")
    half = len(s) // 2
    a = sum(1 for ch in s[:half] if ch in vowels)
    b = sum(1 for ch in s[half:] if ch in vowels)
    return a == b

Java

public static boolean halvesAreAlike(String s) {
    String vowels = "aeiouAEIOU";
    int half = s.length() / 2;
    int a = 0, b = 0;
    for (int i = 0; i < s.length(); i++) {
        if (vowels.indexOf(s.charAt(i)) >= 0) {
            if (i < half) a++;
            else b++;
        }
    }
    return a == b;
}

C++

bool halvesAreAlike(string s) {
    string vowels = "aeiouAEIOU";
    int half = (int) s.size() / 2;
    int a = 0, b = 0;
    for (int i = 0; i < (int) s.size(); i++) {
        if (vowels.find(s[i]) != string::npos) {
            if (i < half) a++;
            else b++;
        }
    }
    return a == b;
}

JavaScript

var halvesAreAlike = function(s) {
    const isVowel = function(c) { return "aeiouAEIOU".indexOf(c) >= 0; };
    const half = s.length / 2;
    let a = 0, b = 0;
    for (let i = 0; i < half; i++) {
        if (isVowel(s[i])) a++;
    }
    for (let i = half; i < s.length; i++) {
        if (isVowel(s[i])) b++;
    }
    return a === b;
};

Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.

All 515 strings problems · the whole catalogue

Learn the technique: Strings