Check if a Number is Balanced Based on Digit Sums
IntuitionThe problem requires checking whether the sum of digits at even indices is equal to the sum of digits at odd indices in a given string representation o
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
The problem requires checking whether the sum of digits at even indices is equal to the sum of digits at odd indices in a given string representation of a number. My first thought is to iterate through the string and maintain two separate sums—one for even indices and one for odd indices. If these sums are equal at the end, the number is balanced.

# Approach
<!-- Describe your approach to solving the problem. -->

1. Initialize two variables, `evenSum` and `oddSum`, to store the sums of digits at even and odd indices, respectively.
2. Iterate through the string representation of the number:
    - If the index is even, add the corresponding digit (converted from char to int) to evenSum.
    - If the index is odd, add it to oddSum.
3. After the loop, compare the two sums. If they are equal, return true; otherwise, return false.

# Complexity
- **Time complexity:**
    - The solution iterates through the string once, processing each digit in $$O(1)$$ time.
    - Hence, the overall complexity is $$O(n)$$, where n is the length of the string.

- **Space complexity:**
    - The algorithm only uses a few integer variables (`evenSum`, `oddSum`, and loop iterators), which take $$O(1)$$ extra space.
    - Thus, the space complexity is $$O(1)$$.

# Code
```cpp []
class Solution {
public:
    bool isBalanced(string num) {
        //  stoi(str)
        int len = num.size();
        int oddSum = 0;
        int evenSum = 0; 
        for (int i = 0; i < len; i++) {
            if (i % 2 == 0) {
                evenSum += stoi(string(1, num[i]));
            }
            else {
                oddSum += stoi(string(1, num[i]));
            }
        }
        if (evenSum == oddSum) {
            return true;
        }
        else {
            return false;
        }
    }
};
```

# Improvment
1. **Efficient Digit Conversion:**
    *  Instead of `stoi(string(1, num[i]))`, which is inefficient, we use `num[i] - '0'` to convert a character to an integer in constant time.
['String', 'C++']