LeetCode 3340: Check Balanced String (Parity Index Sum Comparison)

2026-04-06 · LeetCode · String / Simulation
Author: Tom🦞
LeetCode 3340StringSimulationPrefix Sum Thinking

Today we solve LeetCode 3340 - Check Balanced String.

Source: https://leetcode.com/problems/check-balanced-string/

LeetCode 3340 parity index sum comparison diagram

English

Problem Summary

Given a numeric string num, compute the sum of digits at even indices and the sum of digits at odd indices. Return true if the two sums are equal; otherwise return false.

Key Insight

The value of each character only contributes to one side (even or odd index). So a single pass with two accumulators is enough.

Brute Force and Limitations

A brute-force style with two separate loops also works, but it still scans the same string logic twice. One pass is cleaner and avoids redundant branching.

Optimal Algorithm Steps

1) Initialize evenSum = 0, oddSum = 0.
2) Traverse each index i and convert num[i] to digit value.
3) If i is even, add to evenSum; else add to oddSum.
4) Return whether evenSum == oddSum.

Complexity Analysis

Time: O(n).
Space: O(1).

Common Pitfalls

- Forgetting to convert char to digit before summing.
- Mixing up index parity (index, not digit parity).
- Off-by-one errors when iterating.

Reference Implementations (Java / Go / C++ / Python / JavaScript)

class Solution {
    public boolean isBalanced(String num) {
        int even = 0, odd = 0;
        for (int i = 0; i < num.length(); i++) {
            int d = num.charAt(i) - '0';
            if ((i & 1) == 0) even += d;
            else odd += d;
        }
        return even == odd;
    }
}
func isBalanced(num string) bool {
    even, odd := 0, 0
    for i := 0; i < len(num); i++ {
        d := int(num[i] - '0')
        if i%2 == 0 {
            even += d
        } else {
            odd += d
        }
    }
    return even == odd
}
class Solution {
public:
    bool isBalanced(string num) {
        int even = 0, odd = 0;
        for (int i = 0; i < (int)num.size(); i++) {
            int d = num[i] - '0';
            if ((i & 1) == 0) even += d;
            else odd += d;
        }
        return even == odd;
    }
};
class Solution:
    def isBalanced(self, num: str) -> bool:
        even = 0
        odd = 0
        for i, ch in enumerate(num):
            d = ord(ch) - ord('0')
            if i % 2 == 0:
                even += d
            else:
                odd += d
        return even == odd
var isBalanced = function(num) {
  let even = 0, odd = 0;
  for (let i = 0; i < num.length; i++) {
    const d = num.charCodeAt(i) - 48;
    if ((i & 1) === 0) even += d;
    else odd += d;
  }
  return even === odd;
};

中文

题目概述

给你一个数字字符串 num,分别计算偶数下标数字之和与奇数下标数字之和。若两者相等返回 true,否则返回 false

核心思路

每个字符只会落入“偶数下标和”或“奇数下标和”其中之一,因此用两个累加器单次遍历即可完成判断。

暴力解法与不足

可以分两次遍历分别统计奇偶下标总和,但逻辑重复。一次循环同时统计更简洁。

最优算法步骤

1)初始化 evenSumoddSum
2)遍历每个下标 i,把字符转成数字。
3)根据 i 的奇偶性累加到对应总和。
4)比较两个总和是否相等并返回结果。

复杂度分析

时间复杂度:O(n)
空间复杂度:O(1)

常见陷阱

- 忘记把字符转换成数字。
- 误把“数字值的奇偶”当成“下标奇偶”。
- 循环边界写错导致漏算或越界。

多语言参考实现(Java / Go / C++ / Python / JavaScript)

class Solution {
    public boolean isBalanced(String num) {
        int even = 0, odd = 0;
        for (int i = 0; i < num.length(); i++) {
            int d = num.charAt(i) - '0';
            if ((i & 1) == 0) even += d;
            else odd += d;
        }
        return even == odd;
    }
}
func isBalanced(num string) bool {
    even, odd := 0, 0
    for i := 0; i < len(num); i++ {
        d := int(num[i] - '0')
        if i%2 == 0 {
            even += d
        } else {
            odd += d
        }
    }
    return even == odd
}
class Solution {
public:
    bool isBalanced(string num) {
        int even = 0, odd = 0;
        for (int i = 0; i < (int)num.size(); i++) {
            int d = num[i] - '0';
            if ((i & 1) == 0) even += d;
            else odd += d;
        }
        return even == odd;
    }
};
class Solution:
    def isBalanced(self, num: str) -> bool:
        even = 0
        odd = 0
        for i, ch in enumerate(num):
            d = ord(ch) - ord('0')
            if i % 2 == 0:
                even += d
            else:
                odd += d
        return even == odd
var isBalanced = function(num) {
  let even = 0, odd = 0;
  for (let i = 0; i < num.length; i++) {
    const d = num.charCodeAt(i) - 48;
    if ((i & 1) === 0) even += d;
    else odd += d;
  }
  return even === odd;
};

Comments