Created
May 8, 2018 21:31
-
-
Save BiruLyu/eac277e6651aae5423c4d538cfb88dc4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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