831. Masking Personal Information LeetCode Solution
In this guide, you will get 831. Masking Personal Information LeetCode Solution with the best time and space complexity. The solution to Masking Personal Information problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Masking Personal Information solution in C++
- Masking Personal Information solution in Java
- Masking Personal Information solution in Python
- Additional Resources
Problem Statement of Masking Personal Information
You are given a personal information string s, representing either an email address or a phone number. Return the masked personal information using the below rules.
Email address:
An email address is:
A name consisting of uppercase and lowercase English letters, followed by
The ‘@’ symbol, followed by
The domain consisting of uppercase and lowercase English letters with a dot ‘.’ somewhere in the middle (not the first or last character).
To mask an email:
The uppercase letters in the name and domain must be converted to lowercase letters.
The middle letters of the name (i.e., all but the first and last letters) must be replaced by 5 asterisks “*****”.
Phone number:
A phone number is formatted as follows:
The phone number contains 10-13 digits.
The last 10 digits make up the local number.
The remaining 0-3 digits, in the beginning, make up the country code.
Separation characters from the set {‘+’, ‘-‘, ‘(‘, ‘)’, ‘ ‘} separate the above digits in some way.
To mask a phone number:
Remove all separation characters.
The masked phone number should have the form:
“***-***-XXXX” if the country code has 0 digits.
“+*-***-***-XXXX” if the country code has 1 digit.
“+**-***-***-XXXX” if the country code has 2 digits.
“+***-***-***-XXXX” if the country code has 3 digits.
“XXXX” is the last 4 digits of the local number.
Example 1:
Input: s = “LeetCode@LeetCode.com”
Output: “l*****e@leetcode.com”
Explanation: s is an email address.
The name and domain are converted to lowercase, and the middle of the name is replaced by 5 asterisks.
Example 2:
Input: s = “AB@qq.com”
Output: “a*****b@qq.com”
Explanation: s is an email address.
The name and domain are converted to lowercase, and the middle of the name is replaced by 5 asterisks.
Note that even though “ab” is 2 characters, it still must have 5 asterisks in the middle.
Example 3:
Input: s = “1(234)567-890”
Output: “***-***-7890”
Explanation: s is a phone number.
There are 10 digits, so the local number is 10 digits and the country code is 0 digits.
Thus, the resulting masked number is “***-***-7890”.
Constraints:
s is either a valid email or a phone number.
If s is an email:
8 <= s.length <= 40
s consists of uppercase and lowercase English letters and exactly one '@' symbol and '.' symbol.
If s is a phone number:
10 <= s.length <= 20
s consists of digits, spaces, and the symbols '(', ')', '-', and '+'.
Complexity Analysis
- Time Complexity: O(1)
- Space Complexity: O(1)
831. Masking Personal Information LeetCode Solution in C++
class Solution {
public:
string maskPII(string s) {
const int atIndex = s.find('@');
if (atIndex != string::npos) {
ranges::transform(s, s.begin(), ::tolower);
return s.substr(0, 1) + "*****" + s.substr(atIndex - 1);
}
string ans;
for (const char c : s)
if (isdigit(c))
ans += c;
if (ans.length() == 10)
return "***-***-" + ans.substr(ans.length() - 4);
return '+' + string(ans.length() - 10, '*') + "-***-***-" +
ans.substr(ans.length() - 4);
}
};
/* code provided by PROGIEZ */
831. Masking Personal Information LeetCode Solution in Java
class Solution {
public String maskPII(String s) {
final int atIndex = s.indexOf('@');
if (atIndex > 0) {
s = s.toLowerCase();
return s.charAt(0) + "*****" + s.substring(atIndex - 1);
}
StringBuilder sb = new StringBuilder();
for (final char c : s.toCharArray())
if (Character.isDigit(c))
sb.append(c);
if (sb.length() == 10)
return "***-***-" + sb.substring(sb.length() - 4).toString();
return '+' + "*".repeat(sb.length() - 10) + "-***-***-" +
sb.substring(sb.length() - 4).toString();
}
}
// code provided by PROGIEZ
831. Masking Personal Information LeetCode Solution in Python
class Solution:
def maskPII(self, s: str) -> str:
atIndex = s.find('@')
if atIndex != -1:
s = s.lower()
return s[0] + '*' * 5 + s[atIndex - 1:]
ans = ''.join(c for c in s if c.isdigit())
if len(ans) == 10:
return '***-***-' + ans[-4:]
return '+' + '*' * (len(ans) - 10) + '-***-***-' + ans[-4:]
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.