0831.cpp (1003B)
1 class Solution { 2 public: 3 string maskPII(string &s) const { 4 const auto at = s.find('@'); 5 string res; 6 7 if (at != string::npos) { 8 res += tolower(s[0]); 9 res += "*****"; 10 res += tolower(s[at - 1]); 11 for (int i = at; i < size(s); i++) 12 res += tolower(s[i]); 13 } else { 14 int digits = 0; 15 for (const char c : s) 16 digits += isdigit(c); 17 18 if (digits == 10) 19 res += "***-***-"; 20 else if (digits == 11) 21 res += "+*-***-***-"; 22 else if (digits == 12) 23 res += "+**-***-***-"; 24 else if (digits == 13) 25 res += "+***-***-***-"; 26 27 string extra; 28 for (int i = size(s) - 1; size(extra) < 4; i--) { 29 if (!isdigit(s[i])) continue; 30 extra = s[i] + extra; 31 } 32 33 res += extra; 34 } 35 36 return res; 37 } 38 };