Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save BiruLyu/eac277e6651aae5423c4d538cfb88dc4 to your computer and use it in GitHub Desktop.

Select an option

Save BiruLyu/eac277e6651aae5423c4d538cfb88dc4 to your computer and use it in GitHub Desktop.
class Solution {
public String maskPII(String S) {
StringBuilder res = new StringBuilder();
int idx = S.indexOf('@');
int len = S.length();
if (idx > 0) {
res.append(Character.toLowerCase(S.charAt(0)));
res.append("*****");
res.append(S.substring(idx - 1).toLowerCase());
} else {
int lastFour = 0;
for (int i = len - 1; i >= 0; i--) {
if (Character.isDigit(S.charAt(i))) {
if (lastFour < 4) {
res.insert(0, S.charAt(i));
lastFour++;
} else {
res.insert(0, '*');
}
}
}
int cntOfChar = res.length();
if (cntOfChar - 10 > 0) {
res.insert(0, '+');
cntOfChar += 1;
res.insert(cntOfChar - 10, '-');
cntOfChar += 1;
}
res.insert(cntOfChar - 4, '-');
res.insert(cntOfChar - 7, '-');
}
return res.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment