LeetCode Problem Workspace

Determine if String Halves Are Alike

Check if two halves of a string contain the same number of vowels using a character counting approach efficiently.

category

2

Topics

code_blocks

8

Code langs

hub

3

Related

Practice Focus

Easy · String plus Counting

bolt

Answer-first summary

Check if two halves of a string contain the same number of vowels using a character counting approach efficiently.

Interview AiBox logo

Ace coding interviews with Interview AiBox guidance for String plus Counting

Try AiBox Copilotarrow_forward

Split the given string into two equal halves and count the vowels in each. Compare the vowel counts directly and return true if they match, otherwise false. Use a helper function to detect vowels in both uppercase and lowercase efficiently, ensuring the solution handles the problem pattern of string plus counting without unnecessary iterations.

Problem Statement

You are given a string s of even length. Divide s into two halves of equal size: the first half a and the second half b. Your task is to determine if these halves are alike based on their vowel content.

Two halves are considered alike if they contain the same number of vowels. Vowels include 'a', 'e', 'i', 'o', 'u' and their uppercase versions. Return true if both halves are alike; otherwise, return false.

Examples

Example 1

Input: s = "book"

Output: true

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

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.

Constraints

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

Solution Approach

Split String and Count Vowels

Divide the string into equal halves. Iterate through each half separately and count the vowels using a helper function. Compare the counts to determine if the halves are alike.

Use a Vowel Detection Helper

Implement a function that checks if a character is a vowel considering both lowercase and uppercase. This avoids repeated checks and keeps the counting logic clean and efficient.

Return Comparison Result

After counting vowels in both halves, return true if the counts match and false otherwise. This ensures a single-pass solution per half with O(n) time complexity relative to string length.

Complexity Analysis

Metric Value
Time Depends on the final approach
Space Depends on the final approach

Time complexity is O(n) where n is the length of the string since each half is scanned once. Space complexity is O(1) extra space aside from input since only counters are maintained.

What Interviewers Usually Probe

  • The candidate correctly splits the string into two halves without index errors.
  • The candidate efficiently counts vowels considering case sensitivity.
  • The candidate avoids extra data structures and keeps space usage minimal.

Common Pitfalls or Variants

Common pitfalls

  • Forgetting to include uppercase vowels in the count.
  • Incorrectly dividing the string when the length is even.
  • Returning true or false based on the halves themselves instead of their vowel counts.

Follow-up variants

  • Check if string quarters are alike based on vowel counts in a similar pattern.
  • Determine if halves are alike considering consonant counts instead of vowels.
  • Handle strings with non-alphabetic characters and still count only vowels for comparison.

FAQ

What does 'alike' mean in Determine if String Halves Are Alike?

Two halves are alike if they contain the same number of vowels, including both lowercase and uppercase.

How do I efficiently count vowels in both halves?

Use a helper function that checks if a character is a vowel, then iterate through each half and maintain a counter.

Can the input string have odd length?

No, the problem constraint specifies the string length is always even.

Is it necessary to create two substring copies?

Not strictly; you can use indices to iterate each half directly to save space.

What common mistake should I avoid?

Counting only lowercase vowels or failing to compare the counts correctly between halves are frequent errors.

terminal

Solution

Solution 1: Counting

Traverse the string. If the number of vowels in the first half of the string is equal to the number of vowels in the second half, return `true`. Otherwise, return `false`.

1
2
3
4
5
6
7
8
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
Determine if String Halves Are Alike Solution: String plus Counting | LeetCode #1704 Easy