0 ms Beats 100.00% | Easy Typescript Solution with Explanation
IntuitionApproachComplexity
Time complexity: O(n) where n is the length of the input string num. This is because the function iterates through each character of
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

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

# Complexity
- Time complexity: $$O(n)$$ where n is the length of the input string `num`. This is because the function iterates through each character of the string exactly once, performing constant-time operations (addition and comparison) for each character.
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: $$O(1)$$ as the function uses a fixed amount of space for the variables `odd` and `even`, regardless of the size of the input string. No additional data structures that grow with the input size are used. Thus, the space used remains constant.
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```typescript []
function isBalanced(num: string): boolean {
    let odd: number = 0;
    let even: number = 0;

    for (let i = 0; i < num.length; i++) {

        // if index is even then add current digit in even else in odd
        if (i % 2 === 0) {
            even += +num[i];
        } else {
            odd += +num[i];
        }
    }

    // if odd is even is equal to odd then its balanced string
    return even === odd;
};
```
['String', 'TypeScript']