Linear solution with constant space
Approach\nThe solution is very straightforward, we calculate the sum of digits in even indices and the sum of digits in odd indices. We can optimize things a li
# Approach\nThe solution is very straightforward, we calculate the sum of digits in even indices and the sum of digits in odd indices. We can optimize things a little bit by computing a single sum like: $$num[0] - num[1] + num[2] - num[3] + ...$$, where the final sum should be equal to $$0$$.\n\n# Complexity\n- Time complexity:\n$$O(n)$$\n\n- Space complexity:\n$$O(1)$$\n\n# Code\n```rust []\nimpl Solution {\n    pub fn is_balanced(num: String) -> bool {\n        num.as_bytes().iter().fold((0, 1), |(sum, b), &e| (sum + (e - b\'0\') as i32 * b, b * -1)).0 == 0\n    }\n}\n```\n\n```c++ []\nclass Solution {\npublic:\n    bool isBalanced(string_view num) {\n        int sum = 0;\n        for (int i = 0; i < num.size(); i++) {\n            sum += (i % 2 == 0) ? num[i] - \'0\' : -num[i] + \'0\';\n        }\n        return sum == 0;\n    }\n};\n```
['Rust']