File: determine-if-string-halves-are-alike/solution.py

Date: 2026-06-06

Time: 16:20

Purpose

This file solves LeetCode 1704: Determine if String Halves Are Alike. It owns a single responsibility: given an even-length string, determine whether the first half and second half contain the same number of vowels.

Key Components

determineifstringhalvesare_alike(s: str) -> bool

The sole exported function. Contract:

vowels (local constant)

A set of 10 characters — the five English vowels in both cases: "aeiouAEIOU". Using a set gives O(1) membership tests instead of O(n) scans over a string literal.

Patterns

Dependencies

Imports: None. Pure standard-library Python — no external or internal imports.

Imported by: The "Imported By" list in the prompt is misleading — those are *all* test files across the repo, likely an artifact of the test harness importing a common fixture or conftest, not direct consumers of this function. The only genuine consumer is determine-if-string-halves-are-alike/test_solution.py.

Flow

1. Build the vowel lookup set (10 elements).

2. Compute mid = len(s) // 2 — integer division splits the string into two equal halves.

3. Count vowels in s[:mid] via a generator sum.

4. Count vowels in s[mid:] via a second generator sum.

5. Return whether the two counts are equal.

Both halves are scanned independently in a single pass each — total work is O(n) time, O(1) space (the set is fixed-size).

Invariants

Error Handling

None. The function assumes valid input per LeetCode's constraints. Passing an empty string returns True (both halves have 0 vowels). Passing a non-string would raise a TypeError from the iteration — no explicit guard.

Topics to Explore

Beliefs