algoadvance

Leetcode 831. Masking Personal Information

Problem Statement

You are given a string S that represents personal information. The string can be in one of two formats: an email address or a phone number. You need to mask this personal information in the following way:

  1. Email address:
    • The first character of the name and the domain are preserved.
    • Characters between the first character and the ‘@’ are replaced by 5 asterisks (‘*****’).
    • The domain part (after ‘@’) is unchanged.
  2. Phone number:
    • All digits are replaced by asterisks except the last 4 digits.
    • The remaining digits (after removing country code) are formatted as “--XXXX”.
    • Country code will have ‘+’ followed by digits and will be preserved at the beginning, if present.
    • The number of digits after removing the country code will be 10. The ‘+’ sign and digits in the country code will vary in length depending on the country.

Examples:

  1. Email address: “le@leetcode.com” => “l*****e@leetcode.com”
  2. Phone number: “1(234)567-890” => “--7890”
  3. Phone number with country code: “+86-(10)12345678” => “+86---5678”

Constraints:

Clarifying Questions

  1. Can the phone number contain any delimiters such as spaces, hyphens, parentheses, or dots?
    • Yes, phone numbers can contain any of these delimiters, and you need to ignore them when counting digits.
  2. Should the email addresses have exactly one ‘@’?
    • Yes, this problem assumes the email addresses are well-formed with one ‘@’.

Strategy

  1. Identify the Input Type:
    • If S contains ‘@’, it’s an email.
    • Otherwise, it’s a phone number.
  2. Mask Email:
    • Split the email into the name and domain parts using the ‘@’ character.
    • Preserve the first and last characters of the name, replace the intermediate characters with “*****”.
    • Reconstruct and return the masked email.
  3. Mask Phone Number:
    • Remove all non-digit characters from the input phone number.
    • If it starts with ‘+’, count the length of the country code part.
    • Reconstruct the phone number in the format with “--XXXX” and add the country code in as “+CC-“.

Code

#include <iostream>
#include <string>
#include <cctype>

using namespace std;

class Solution {
public:
    string maskPII(string S) {
        // Check if the string is an email or phone number
        if (S.find('@') != string::npos) {
            return maskEmail(S);
        } else {
            return maskPhone(S);
        }
    }
    
private:
    string maskEmail(const string& email) {
        int atPos = email.find('@');
        string local = email.substr(0, atPos);
        string domain = email.substr(atPos);
        
        string maskedEmail = local[0] + string(5, '*') + local.back() + domain;
        return maskedEmail;
    }
    
    string maskPhone(const string& phone) {
        string digits;
        
        // Extract only digits from the phone number
        for (char c : phone) {
            if (isdigit(c)) {
                digits += c;
            }
        }
        
        // The last 4 digits will remain
        string last4 = digits.substr(digits.size() - 4);
        
        // Depending on the length of digits, determine country code and masking
        string maskedPhone;
        int n = digits.size();
        if (n == 10) {
            maskedPhone = "***-***-" + last4;
        } else if (n > 10) {
            string countryCode = "+" + string(n - 10, '*') + "-";
            maskedPhone = countryCode + "***-***-" + last4;
        }
        
        return maskedPhone;
    }
};

// Example Usage
int main() {
    Solution solution;
    cout << solution.maskPII("le@leetcode.com") << endl; // Output: "l*****e@leetcode.com"
    cout << solution.maskPII("1(234)567-890") << endl;    // Output: "***-***-7890"
    cout << solution.maskPII("+86-(10)12345678") << endl; // Output: "+86-***-***-5678"
    
    return 0;
}

Time Complexity

By organizing the problem and solution in this structured manner, it becomes easier to understand and implement.

Cut your prep time in half and DOMINATE your interview with AlgoAdvance AI