Skip to content

Latest commit

 

History

History
23 lines (21 loc) · 586 Bytes

1805.md

File metadata and controls

23 lines (21 loc) · 586 Bytes

1805. Number of Different Integers in a String

Solution 1 (time O(n), space O(1))

class Solution(object):
    def numDifferentIntegers(self, word):
        """
        :type word: str
        :rtype: int
        """
        num_set = set([])
        cur_num = ""
        for i, c in enumerate(word):
            if c.isnumeric():
                cur_num += c
            elif cur_num:
                num_set.add(int(cur_num))
                cur_num = ""
        if cur_num:
            num_set.add(int(cur_num))
        return len(num_set)