MaskingPersonalInformation [source code]
public class MaskingPersonalInformation {
static
/****************************************************************************/
class Solution {
public String maskPII(String S) {
if (S.contains ("@"))
return verifyEmail (S);
else
return verifyPhone (S);
}
String verifyEmail (String S) {
String res = "";
if (!S.matches ("[a-zA-Z]+@[a-zA-Z]+.[a-zA-Z]+"))
return res;
int first_at = S.indexOf ("@"), last_at = S.indexOf ("@"),
first_dot = S.indexOf ("."), last_dot = S.lastIndexOf (".");
if (first_at == last_at && first_at >= 0 && first_dot == last_dot && first_dot >= 0 && first_dot - first_at > 1) {
S = S.toLowerCase ();
res = String.format ("%c*****%c%s", S.charAt (0), S.charAt (first_at - 1), S.substring (first_at));
}
return res;
}
String verifyPhone (String S) {
String res = "";
S = S.replaceAll ("[^0-9]", "");
if (S.length () >= 10) {
int len = S.length ();
res = String.format ("%s***-***-%s", len > 10 ? "+" + stars (len - 10) + "-" : "", S.substring (len - 4));
}
return res;
}
String stars (int num) {
StringBuilder sb = new StringBuilder ();
for (int i = 0; i < num; i++)
sb.append ("*");
return sb.toString ();
}
}
/****************************************************************************/
public static void main(String[] args) {
MaskingPersonalInformation.Solution tester = new MaskingPersonalInformation.Solution();
String[] inputs = {
"[email protected]", "l*****[email protected]",
"[email protected]", "a*****[email protected]",
"1(234)567-890", "***-***-7890",
"86-(10)12345678", "+**-***-***-5678",
};
for (int i = 0; i < inputs.length / 2; i++) {
System.out.println (Printer.separator ());
String S = inputs[2 * i], ans = inputs[2 * i + 1],
output = tester.maskPII (S);
System.out.printf ("%s\n%s\n%s\n",
S, Printer.wrap (output, output.equals (ans) ? 92 : 91), ans
);
}
}
}
常规特殊返回值要熟悉:
java> String s = "abc"
java.lang.String s = "abc"
java> s.indexOf ("d")
java.lang.Integer res1 = -1
题目比较啰嗦, 还考察正则; 不过最后AC的倒是很快, 关键是要思路清晰, 尤其是看我这里的两个verify函数的设计;
editorial
Approach #1: Direct [Accepted]
Intuition and Algorithm
We perform the algorithm as described.
First, to check if information is an email, we check whether it contains a '@'. (There are many different tests: we could check for letters versus digits, for example.)
If we have an email, we should replace the first name with the first letter of that name, followed by 5 asterisks, followed by the last letter of that name.
If we have a phone number, we should collect all the digits and then format it according to the description.
class Solution {
public String maskPII(String S) {
int atIndex = S.indexOf('@');
if (atIndex >= 0) { // email
return (S.substring(0, 1) + "*****" + S.substring(atIndex - 1)).toLowerCase();
} else { // phone
String digits = S.replaceAll("\\D+", "");
String local = "***-***-" + digits.substring(digits.length() - 4);
if (digits.length() == 10) return local;
String ans = "+";
for (int i = 0; i < digits.length() - 10; ++i)
ans += "*";
return ans + "-" + local;
}
}
}
这代码好短啊;
他这里判断只要有一个@就可以. 但是我是判断了至多只有一个@的; 而且我还判断必须有.
. 他这里也没有判断, 也过了; 题目有说这些无所谓吗? 好像没有吧;
他确实完全没有判断.
到底有没有, 以及到底有几个.
, 几个@.
\D A non-digit: [^0-9]
对于phone部分, 这个是他用的这个正则; 我放在stars里面的东西, 他本地一个for循环解决了; 总体思路反正是类似的;
我这题最后代码有点啰嗦一个原因是我检查的东西其实更多; 我感觉是不是题目没有描述清楚? 他这里忽视掉的很多东西, 题目里面没有暗示对应的assumption吧;
UNFINISHED
uwi: 因为题目太长他放到最后一题写的, 7min
class Solution {
public String maskPII(String S) {
if(S.indexOf("@") >= 0){
// mail
S = S.toLowerCase();
int x = S.indexOf("@");
return "" + S.charAt(0) + "*****" + S.substring(x-1);
}else{
// phone
String last = "";
int digit = 0;
for(int i = S.length()-1;i >= 0;i--){
if(S.charAt(i) >= '0' && S.charAt(i) <= '9'){
if(last.length() < 4){
last = S.charAt(i) + last;
}
digit++;
}
}
if(digit == 10){
return "***-***-" + last;
}else{
String ret = "+";
for(int i = 0;i < digit-10;i++)ret += "*";
return ret + "-***-***-" + last;
}
}
}
}
跟awice的写法非常类似, 对email的判断也是很简单, 只看一个@, 为什么;
对于phone的部分, 他没有用正则;
奇怪了, 这么简单的一个算法他居然用了7分钟, 不太像uwi; 可能其他三题写的太快了, 最后一题边写边玩的;
Problem Description
We are given a personal information string S, which may represent either an email address or a phone number.
We would like to mask this personal information according to the following rules:
- Email address:
We define a name to be a string of length ≥ 2 consisting of only lowercase letters a-z or uppercase letters A-Z.
An email address starts with a name, followed by the symbol '@', followed by a name, followed by the dot '.' and followed by a name.
All email addresses are guaranteed to be valid and in the format of "[email protected]".
To mask an email, all names must be converted to lowercase and all letters between the first and last letter of the first name must be replaced by 5 asterisks '*'
.
- Phone number:
A phone number is a string consisting of only the digits 0-9 or the characters from the set {'+', '-', '(', ')', ' '}. You may assume a phone number contains 10 to 13 digits.
The last 10 digits make up the local number, while the digits before those make up the country code. Note that the country code is optional. We want to expose only the last 4 digits and mask all other digits.
The local number should be formatted and masked as "***-***-1111"
, where 1 represents the exposed digits.
To mask a phone number with country code like "+111 111 111 1111", we write it in the form "+***-***-***-1111"
. The '+' sign and the first '-' sign before the local number should only exist if there is a country code. For example, a 12 digit phone number mask should start with "+**-"
.
Note that extraneous characters like "(", ")", " ", as well as extra dashes or plus signs not part of the above formatting scheme should be removed.
Return the correct "mask" of the information provided.
Example 1:
Input: "[email protected]"
Output: "l*****[email protected]"
Explanation: All names are converted to lowercase, and the letters between the
first and last letter of the first name is replaced by 5 asterisks.
Therefore, "leetcode" -> "l*****e".
Example 2:
Input: "[email protected]"
Output: "a*****[email protected]"
Explanation: There must be 5 asterisks between the first and last letter
of the first name "ab". Therefore, "ab" -> "a*****b".
Example 3:
Input: "1(234)567-890"
Output: "***-***-7890"
Explanation: 10 digits in the phone number, which means all digits make up the local number.
Example 4:
Input: "86-(10)12345678"
Output: "+**-***-***-5678"
Explanation: 12 digits, 2 digits for country code and 10 digits for local number.
Notes:
- S.length <= 40.
- Emails have length at least 8.
- Phone numbers have length at least 10.
Difficulty:Medium
Total Accepted:1.2K
Total Submissions:2.8K
Contributor:awice
Companies
twitter
Related Topics
string