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.
This approach involves iterating over the string and maintaining two sums: one for digits at even indices and another for digits at odd indices. By the end of the iteration, compare the two sums to determine if the string is balanced.
In this solution, we use the strlen function to get the length of the string. We iterate over each character of the string, converting it to an integer by subtracting '0', and update either evenSum or oddSum based on whether the index is even or odd. Finally, we return whether these sums are equal.
Time Complexity: O(n), where n is the length of the string, as we iterate over the string once.
Space Complexity: O(1), since we only use a fixed amount of extra space for the sums.
Instead of calculating sums in a single pass, we can briefly check each character's index. This involves conditional logic within a loop to segregate digits to their corresponding index-based sums.
Here, the calculation of evenSum and oddSum is divided by checking if the index is even or odd. This function relies on the loop running until the null character, ensuring we don't run out of bounds.
Time Complexity: O(n), with n as the length of the string.
Space Complexity: O(1), used space is invariant to input size.
We can use an array f of length 2 to record the sum of numbers at even indices and odd indices. Then, we traverse the string nums and add the numbers to the corresponding positions based on the parity of the indices. Finally, we check whether f[0] is equal to f[1].
The time complexity is O(n), where n is the length of the string nums. The space complexity is O(1).
Python
Java
C++
Go
TypeScript
Rust
JavaScript
C#
PHP
Scala
| Approach | Complexity |
|---|---|
| Simple Iterative Sum Calculation | Time Complexity: O(n), where n is the length of the string, as we iterate over the string once. |
| Index Check and Conditional Sum | Time Complexity: O(n), with n as the length of the string. |
| Simulation | — |
| 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. |
Check Balanced String | Leetcode 3340 • Technosage • 2,397 views views
Watch 5 more video solutions →Practice Check Balanced String with our built-in code editor and test cases.
Practice on FleetCode