Watch 10 video solutions for Determine if String Halves Are Alike, a easy level problem involving String, Counting. This walkthrough by codestorywithMIK has 11,525 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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.
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.
Constraints:
2 <= s.length <= 1000s.length is even.s consists of uppercase and lowercase letters.Problem Overview: You receive a string s with even length. Split it into two halves and determine whether both halves contain the same number of vowels. Vowels include a, e, i, o, u in both lowercase and uppercase. The task reduces to counting vowels in each half and comparing the totals.
Approach 1: Character Counting via Direct Comparison (O(n) time, O(1) space)
Traverse the string and count vowels separately for the first and second halves. Compute the midpoint using mid = n / 2, then iterate from 0 → mid-1 to count vowels in the first half and from mid → n-1 for the second half. Each character is checked against a small set of vowels using direct comparison or membership lookup. Since the vowel set is constant in size, every check runs in constant time, giving a total time complexity of O(n) and constant auxiliary space O(1). This approach is simple and very readable, making it ideal when clarity matters more than minimizing passes.
Approach 2: Two-Pointer Vowel Count Approach (O(n) time, O(1) space)
Use two pointers that move through the two halves simultaneously. One pointer starts at the beginning of the string, and the other starts at the midpoint. On each iteration, check whether each pointer points to a vowel and update two counters (or maintain a single difference counter). This processes both halves in a single loop while performing constant-time vowel checks using a predefined set like "aeiouAEIOU". The algorithm still scans each character once, so the time complexity remains O(n) with O(1) extra space. The advantage is fewer loops and a symmetric structure that mirrors the two halves being compared.
Both strategies rely on fundamental string traversal and simple counting logic. No additional data structures are required because the problem only tracks a small number of counters and checks membership in a constant-size vowel set.
Recommended for interviews: The two-pointer vowel counting approach is typically preferred. It demonstrates clean iteration logic and efficient comparison of both halves in a single pass. Showing the direct counting version first can communicate your understanding quickly, while the two-pointer refinement shows awareness of cleaner traversal patterns often expected in string problems.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Character Counting via Direct Comparison | O(n) | O(1) | When clarity and straightforward logic are preferred; good baseline solution |
| Two-Pointer Vowel Count Approach | O(n) | O(1) | When comparing two segments simultaneously with cleaner single-pass traversal |