Add Numbers in Unclean Data
Given a string such as as+v123bjh^-25j6
, return the operation of the elements in the string summed together. In this case, it would be 123 - 25 + 6
.
The Idea: Parse the string by all digits, or the - seperator. We can ignore + since numbers are by default assumed to be positive. Then take this list of patterns, and filter out everything by the delimitors, which in our case includes the digits and the sign. Finally, iterate through the filtered list, and sum the elements. Keep a flag for negative tokens.
Complexity: O(n) time and space
Last updated