LeetCode 831: Masking Personal Information Solution
Master LeetCode problem 831 (Masking Personal Information), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
831. Masking Personal Information
Problem Explanation
Explanation:
- We need to mask the personal information string based on whether it represents an email address or a phone number.
- For email addresses, we need to convert the name and domain to lowercase and replace the middle characters of the name with asterisks.
- For phone numbers, we need to remove separation characters and mask the number accordingly.
:
Solution Code
class Solution {
public String maskPII(String s) {
if (s.contains("@")) {
return maskEmail(s);
} else {
return maskPhoneNumber(s);
}
}
private String maskEmail(String email) {
int atIndex = email.indexOf('@');
String name = email.substring(0, atIndex).toLowerCase();
String domain = email.substring(atIndex).toLowerCase();
return name.charAt(0) + "*****" + name.charAt(name.length() - 1) + domain;
}
private String maskPhoneNumber(String phone) {
String digits = phone.replaceAll("\\D", "");
int localLength = digits.length() - 10;
String masked = "***-***-" + digits.substring(digits.length() - 4);
if (localLength > 0) {
String country = "+";
for (int i = 0; i < localLength; i++) {
country += "*";
}
return country + "-" + masked;
}
return masked;
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 831 (Masking Personal Information)?
This page provides optimized solutions for LeetCode problem 831 (Masking Personal Information) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 831 (Masking Personal Information)?
The time complexity for LeetCode 831 (Masking Personal Information) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 831 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 831 in Java, C++, or Python.