Leetcode 858 Solution

This article provides solution to leetcode question 858 (masking-personal-information)

https://leetcode.com/problems/masking-personal-information

Solution

class Solution:
    def maskPII(self, s: str) -> str:
        if '@' in s:
            s = s.lower()

            name, domain = s.split('@')
            name = name[0] + "*****" + name[-1]
            return "{}@{}".format(name, domain)
        else:
            numbers = ""
            for ch in s:
                if '0' <= ch <= '9':
                    numbers += ch
            if len(numbers) == 10:
                return "***-***-{}".format(numbers[-4:])
            elif len(numbers) == 11:
                return "+*-***-***-{}".format(numbers[-4:])
            elif len(numbers) == 12:
                return "+**-***-***-{}".format(numbers[-4:])
            elif len(numbers) == 13:
                return "+***-***-***-{}".format(numbers[-4:])