Skip to content

Latest commit

 

History

History
26 lines (24 loc) · 671 Bytes

1694.md

File metadata and controls

26 lines (24 loc) · 671 Bytes

1694. Reformat Phone Number

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

class Solution(object):
    def reformatNumber(self, number):
        """
        :type number: str
        :rtype: str
        """
        ans = []
        cur_seg = ""
        for c in number:
            if c.isdigit():
                cur_seg += c
                if len(cur_seg) == 3:
                    ans.append(cur_seg)
                    cur_seg = ""
        if len(cur_seg) == 1:
            t = ans.pop()
            ans.extend([t[:2], t[-1] + cur_seg])
        elif len(cur_seg) >= 2:
            ans.append(cur_seg)
        return "-".join(ans)