Problem statement

https://binarysearch.com/problems/ASCII-String-to-Integer/

Solution

We can use groupby here.

Complexity

It is O(n) for time and space.

Code

class Solution:
    def solve(self, s):
        B = ["".join(x) for _, x in groupby(s, key=lambda x: x.isdigit())]
        ans = 0
        for x in B:
            if x.isdigit(): ans += int(x)
        return ans