Skip to main content

Check Balanced String - Solution & Explanation

EasyString15 min readAsked at: Amazon
Practice this problem

Problem Statement

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:

  • The sum of digits at even indices is 1 + 3 == 4, and the sum of digits at odd indices is 2 + 4 == 6.
  • Since 4 is not equal to 6, num is not balanced.

Example 2:

Input: num = "24123"

Output: true

Explanation:

  • The sum of digits at even indices is 2 + 1 + 3 == 6, and the sum of digits at odd indices is 4 + 2 == 6.
  • Since both are equal the num is balanced.

 

Constraints:

  • 2 <= num.length <= 100
  • num consists of digits only

Approach Overview

Problem 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 1: Simple Iterative Sum Calculation

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.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

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.

Try this approach in the editor →

Approach 2: Index Check and Conditional Sum

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.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n), with n as the length of the string.
Space Complexity: O(1), used space is invariant to input size.

Try this approach in the editor →

Approach 3: Simulation

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).

Code

Python

Java

C++

Go

TypeScript

Rust

JavaScript

C#

PHP

Scala

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Simple Iterative Sum Calculation

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.

Index Check and Conditional Sum

Time Complexity: O(n), with n as the length of the string.
Space Complexity: O(1), used space is invariant to input size.

Simulation—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Simple Iterative Sum CalculationO(n)O(1)Best general solution. Clean and readable single-pass logic.
Index Check and Conditional SumO(n)O(1)Useful when implementing compact loops or competitive programming solutions.

Video Solution

Check Balanced String | Leetcode 3340 • Technosage • 2,733 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Check Balanced String easy or hard?
Check Balanced String is categorized as an Easy problem. The solution only requires basic string traversal, digit conversion, and conditional checks based on index parity.
Check Balanced String Python/Java solution
Both Python and Java implementations use the same logic: loop through the string, convert characters to digits, and update the correct sum based on index parity. The algorithm remains O(n) time and O(1) space across all languages including C++, JavaScript, and C#.
How to solve Check Balanced String in O(n)?
Iterate through the string from index 0 to n-1. Convert each character to its numeric value and add it to either the even-index sum or odd-index sum depending on i % 2. After the loop finishes, compare the two sums to determine whether the string is balanced.
What is the best approach for Check Balanced String?
The best approach is a single-pass iteration through the string while maintaining two sums for even and odd indices. Convert each character to a digit and update the correct sum based on index parity. This method runs in O(n) time and uses O(1) extra space.
Is Check Balanced String asked at Google/Amazon/Meta?
This problem reflects common string iteration and index-parity logic often seen in screening rounds. While the exact problem ID may not appear frequently, similar parity-sum and string traversal questions are common in interviews at large tech companies.
What data structure is used in Check Balanced String?
No additional data structure is required. The solution operates directly on the input string and uses two integer variables to track the running sums for even and odd indices.
What is the time complexity of Check Balanced String?
The time complexity is O(n) because the algorithm scans the string exactly once. Each character is processed with constant-time operations such as digit conversion and addition. Space complexity remains O(1) since only two counters are stored.

Ready to solve this problem?

Practice Check Balanced String with our built-in code editor and test cases.

Practice on FleetCode