class Solution:
    def maskPII(self, S):
        """
        :type S: str
        :rtype: str
        """
        if '@' in S:    # email
            name, postfix = S.lower().split('@')
            return name[0] + '*****' + name[-1] + '@' + postfix
        else:
            chars = {'+', '-', '(', ')', ' '}
            digits = []
            for c in S:
                if c not in chars:
                    digits.append(c)
            prefix = ''
            if len(digits) > 10:
                prefix = '+' + ('*' * (len(digits) - 10)) + '-'
            return prefix + '***-***-' + ''.join(digits[-4:])