Problem statement

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

Solution

Quite easy problem: first check if @ is inside string: if it is, we have email and then we split it, add mask and put it to lower. If it is number, we extract only digits, check if we have 10 of them or more and add county code if needed.

Complexity

Time and space complexity is O(n).

Code

class Solution:
    def maskPII(self, S):
        if "@" in S:
            p1, p2 = S.split("@")
            return (p1[0] + "*"*5 + p1[-1] + "@" + p2).lower()
        else:
            digits = [i for i in S if i.isdigit()]
            country = "" if len(digits) == 10 else "+" + "*"*(len(digits) - 10) + "-"
            return country + "***-***-" + "".join(digits[-4:])