Talentd
FleetCode

3340. Check Balanced String

Easy82.3% acceptance1 company

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

Asked at 1 company

Topics

7:03
Still stuck?Read 3 approaches, or watch a walkthrough.
Open solution

Similar problems

Balanced Binary TreeEasy
Loading editor…
1class Solution:
2 def isBalanced(self, num: str) -> bool:
3
Starter codePython 3Spaces: 4
Examples
Input
"1234"
Input
"24123"
Your code is saved on this device.Solution