classSolution { String[] country = {"", "+*-", "+**-", "+***-"};
public String maskPII(String s) { intat= s.indexOf("@"); if (at > 0) { s = s.toLowerCase(); return (s.charAt(0) + "*****" + s.substring(at - 1)).toLowerCase(); } s = s.replaceAll("[^0-9]", ""); return country[s.length() - 10] + "***-***-" + s.substring(s.length() - 4); } }
[sol1-C#]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
publicclassSolution { string[] country = {"", "+*-", "+**-", "+***-"};
publicstringMaskPII(string s) { int at = s.IndexOf("@"); if (at > 0) { s = s.ToLower(); return (s[0] + "*****" + s.Substring(at - 1)).ToLower(); } StringBuilder sb = new StringBuilder(); foreach (char c in s) { if (char.IsDigit(c)) { sb.Append(c); } } s = sb.ToString(); return country[s.Length - 10] + "***-***-" + s.Substring(s.Length - 4); } }
[sol1-Python3]
1 2 3 4 5 6 7
classSolution: defmaskPII(self, s: str) -> str: at = s.find('@') if at >= 0: return (s[0] + "*" * 5 + s[at - 1:]).lower() s = "".join(i for i in s if i.isdigit()) return ["", "+*-", "+**-", "+***-"][len(s) - 10] + "***-***-" + s[-4:]
[sol1-Go]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
funcmaskPII(s string)string { at := strings.Index(s, "@") if at > 0 { s = strings.ToLower(s) return strings.ToLower(string(s[0])) + "*****" + s[at-1:] } var sb strings.Builder for i := 0; i < len(s); i++ { c := s[i] if unicode.IsDigit(rune(c)) { sb.WriteByte(c) } } s = sb.String() country := []string{"", "+*-", "+**-", "+***-"} return country[len(s)-10] + "***-***-" + s[len(s)-4:] }
[sol1-JavaScript]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
const country = ["", "+*-", "+**-", "+***-"];
var maskPII = function(s) { const at = s.indexOf("@"); if (at > 0) { s = s.toLowerCase(); return (s[0] + "*****" + s.substring(at - 1)).toLowerCase(); } let sb = ""; for (let i = 0; i < s.length; i++) { const c = s.charAt(i); if ('0' <= c && c <= '9') { sb += c; } } s = sb.toString(); return country[s.length - 10] + "***-***-" + s.substring(s.length - 4); };