Watch 6 video solutions for Check Balanced String, a easy level problem involving String. This walkthrough by Technosage has 2,397 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given a string num consisting of only digits. A string of digits is called balanced if the sum of the digits at even indices is equal to the sum of digits at odd indices.
Return true if num is balanced, otherwise return false.
Example 1:
Input: num = "1234"
Output: false
Explanation:
1 + 3 == 4, and the sum of digits at odd indices is 2 + 4 == 6.num is not balanced.Example 2:
Input: num = "24123"
Output: true
Explanation:
2 + 1 + 3 == 6, and the sum of digits at odd indices is 4 + 2 == 6.num is balanced.
Constraints:
2 <= num.length <= 100num consists of digits onlyProblem Overview: You are given a numeric string. A string is considered balanced when the sum of digits at even indices equals the sum of digits at odd indices. The task is to compute both sums and return whether they are equal.
Approach 1: Simple Iterative Sum Calculation (O(n) time, O(1) space)
The most direct approach is to scan the string once and maintain two running totals: one for even indices and one for odd indices. For every character s[i], convert it to its numeric value using s[i] - '0' (or equivalent in your language). If i % 2 == 0, add the digit to the even sum; otherwise add it to the odd sum. After processing the entire string, compare the two totals. If they match, the string is balanced.
This approach works because the problem only requires a parity-based partition of indices. There is no need for additional data structures or preprocessing. The algorithm performs a single pass through the string, making the time complexity O(n) and the extra space usage O(1). This is the simplest and most readable implementation, which makes it ideal for interviews and production code where clarity matters.
Approach 2: Index Check and Conditional Sum (O(n) time, O(1) space)
This variation keeps the same single-pass idea but focuses on the index parity check as the core operation. Iterate through the string and use a conditional branch based on the index: if the index is even, accumulate into one variable; otherwise accumulate into another. The digit extraction and accumulation happen in the same loop, so the entire computation remains linear.
The key insight is that the position of each character determines which group it contributes to. No sorting, extra arrays, or prefix structures are needed. This keeps memory usage constant while maintaining a straightforward implementation. The algorithm again runs in O(n) time with O(1) auxiliary space.
This style is sometimes preferred when writing tight loops or implementing quick checks in competitive programming, because the logic stays compact and predictable. It is essentially a lightweight simulation of the rule that defines a balanced string.
Recommended for interviews: The single-pass iterative approach is exactly what interviewers expect. A brute-force alternative doesn't really exist here because the optimal strategy is already straightforward. Showing a clean loop over the string, careful digit conversion, and correct index parity handling demonstrates solid fundamentals and attention to detail.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Simple Iterative Sum Calculation | O(n) | O(1) | Best general solution. Clean and readable single-pass logic. |
| Index Check and Conditional Sum | O(n) | O(1) | Useful when implementing compact loops or competitive programming solutions. |