pesudo code
1
decide if it is an email or phone
2-1 (email case)
Split string array into 3 strings
3-1
Get the first element of a divided string array
4-1
Extracting the first and last characters of an imported string
5-1
Put 5 astarisks in the extracted string and return
6-1
Concatenate the created string with the rest of the string array and return it
2-2 (phone case)
Create a phone number by extracting only numbers with ASCII code,
and check the presence or absence of a country code
by obtaining the number of digits of the phone number.
3-2
In case of country code with +,-, etc.
Separating the rest of the last 10 digits by country code
4-2
Separate 10 numbers into a string array with lengths of 3,3,4
5-2
Replace the first and second elements of the divided array with Astaris
6-2
The separated country code and area code are combined with-and returned.
3-3
If the country code is not marked with +,-, etc.
Separate 10 numbers into a string array with lengths of 3,3,4
4-3
Replace the first and second elements of the divided array with Astaris
5-3
Returned by appending '-' to the substituted string
1차 시도
class Solution {
public String maskPII(String S) {
boolean check = false;
check = S.contains("@");
if(check) {
// Email Case
S = S.toLowerCase();
String[] emailArr = new String[3];
emailArr = seperateEmail(emailArr, S);
emailArr[0] = appendingAsterisk(emailArr[0]);
return makeEmail(emailArr);
}else {
// Phone Case
String temp = extractNumber(S);
int numberLength = temp.length();
if(numberLength < 12){
// has not Country Code
String[] numberArr = new String[3];
if(numberLength == 10){
numberArr[0] = temp.substring(0,3);
numberArr[1] = temp.substring(3,6);
numberArr[2] = temp.substring(6,10);
}else {
numberArr[0] = temp.substring(0,3);
numberArr[1] = temp.substring(3,7);
numberArr[2] = temp.substring(7,11);
}
return makePhoneNumber(numberArr);
}else {
// has Country Code
String[] numberArr = new String[4];
if(numberLength == 12){
numberArr[0] = temp.substring(0,2);
numberArr[1] = temp.substring(2,5);
numberArr[2] = temp.substring(5,8);
numberArr[3] = temp.substring(8,12);
}else {
numberArr[0] = temp.substring(0,3);
numberArr[1] = temp.substring(3,6);
numberArr[2] = temp.substring(6,9);
numberArr[3] = temp.substring(9,13);
}
return makePhoneNumber(numberArr);
}
}
}
public String[] seperateEmail(String[] emailArr, String s) {
String[] temp = s.split("@");
emailArr[0] = temp[0];
temp = temp[1].split("\\.");
emailArr[1] = temp[0];
emailArr[2] = temp[1];
return emailArr;
}
public String appendingAsterisk(String first) {
String temp = "";
temp += first.charAt(0);
temp += "*****";
temp += first.charAt(first.length()-1);
return temp;
}
public String makeEmail(String[] emailArr){
String temp = "";
temp += emailArr[0];
temp += "@";
temp += emailArr[1];
temp += ".";
temp += emailArr[2];
return temp;
}
public String extractNumber(String s){
// 0 ~ 9 ASCII CODE -> 48 ~ 57
String temp = "";
for(int i = 0; i < s.length(); i++) {
if(48 <=s.charAt(i) && s.charAt(i) <= 57) {
temp += s.charAt(i);
}
}
return temp;
}
public String makePhoneNumber(String[] numberArr) {
String temp = "";
String middle = "";
if(numberArr.length < 4){
middle = makeAsterisk(numberArr[1].length());
temp += "***-"+middle+"-"+numberArr[2];
}else{
temp += "+"+makeAsterisk(numberArr[0].length());
middle = makeAsterisk(numberArr[2].length());
temp += "-***-"+middle+"-"+numberArr[3];
}
return temp;
}
public String makeAsterisk(int number) {
String asterisk = "";
for(int i = 0; i < number; i++) {
asterisk += "*";
}
return asterisk;
}
}
Error Case
Input: "+86(88)1513-7-74"
Output: "***-****-3774"
Expected: "+*-***-***-3774"
2차시도
class Solution {
public String maskPII(String S) {
boolean check = false;
check = S.contains("@");
if(check) {
// Email Case
S = S.toLowerCase();
String[] emailArr = new String[3];
emailArr = seperateEmail(emailArr, S);
emailArr[0] = appendingAsterisk(emailArr[0]);
return makeEmail(emailArr);
}else {
// Phone Case
String temp = extractNumber(S);
int numberLength = temp.length();
if(numberLength < 11){
// has not Country Code
String[] numberArr = new String[3];
numberArr[0] = temp.substring(0,3);
numberArr[1] = temp.substring(3,6);
numberArr[2] = temp.substring(6,10);
return makePhoneNumber(numberArr);
}else {
// has Country Code
String[] numberArr = new String[4];
if(numberLength == 11){
numberArr[0] = temp.substring(0,1);
numberArr[1] = temp.substring(1,4);
numberArr[2] = temp.substring(4,7);
numberArr[3] = temp.substring(7,11);
}else if(numberLength == 12){
numberArr[0] = temp.substring(0,2);
numberArr[1] = temp.substring(2,5);
numberArr[2] = temp.substring(5,8);
numberArr[3] = temp.substring(8,12);
}else {
numberArr[0] = temp.substring(0,3);
numberArr[1] = temp.substring(3,6);
numberArr[2] = temp.substring(6,9);
numberArr[3] = temp.substring(9,13);
}
return makePhoneNumber(numberArr);
}
}
}
public String[] seperateEmail(String[] emailArr, String s) {
String[] temp = s.split("@");
emailArr[0] = temp[0];
temp = temp[1].split("\\.");
emailArr[1] = temp[0];
emailArr[2] = temp[1];
return emailArr;
}
public String appendingAsterisk(String first) {
String temp = "";
temp += first.charAt(0);
temp += "*****";
temp += first.charAt(first.length()-1);
return temp;
}
public String makeEmail(String[] emailArr){
String temp = "";
temp += emailArr[0];
temp += "@";
temp += emailArr[1];
temp += ".";
temp += emailArr[2];
return temp;
}
public String extractNumber(String s){
// 0 ~ 9 ASCII CODE -> 48 ~ 57
String temp = "";
for(int i = 0; i < s.length(); i++) {
if(48 <=s.charAt(i) && s.charAt(i) <= 57) {
temp += s.charAt(i);
}
}
return temp;
}
public String makePhoneNumber(String[] numberArr) {
String temp = "";
String middle = "";
if(numberArr.length < 4){
middle = makeAsterisk(numberArr[1].length());
temp += "***-"+middle+"-"+numberArr[2];
}else{
temp += "+"+makeAsterisk(numberArr[0].length());
middle = makeAsterisk(numberArr[2].length());
temp += "-***-"+middle+"-"+numberArr[3];
}
return temp;
}
public String makeAsterisk(int number) {
String asterisk = "";
for(int i = 0; i < number; i++) {
asterisk += "*";
}
return asterisk;
}
}
Runtime: 11 ms
Memory Usage: 38.1 MB
0MS 답안
class Solution {
public String maskPII(String S) {
S = S.toLowerCase();
int idx = S.indexOf('@');
StringBuilder res = new StringBuilder();
if (idx > 0) {
res.append(S.charAt(0));
res.append("*****");
res.append(S.charAt(idx-1));
res.append(S.substring(idx));
} else {
StringBuilder digits = new StringBuilder();
for (char c : S.toCharArray())
if (Character.isDigit(c)) digits.append(c);
if (digits.length() == 10) {
res.append("***-***-");
res.append(digits.substring(6));
} else {
res.append('+');
for (int i = 0; i < digits.length()-10; ++i) res.append('*');
res.append('-');
res.append("***-***-");
res.append(digits.substring(digits.length()-4));
}
}
return res.toString();
}
}
제공하는 답안
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;
}
}
}
'Algorithm > LeetCode' 카테고리의 다른 글
[LeetCode] - Maximum Depth of Binary Tree (0) | 2021.04.26 |
---|---|
[LeetCode] - Height Checker (0) | 2021.04.23 |
[LeetCode] - Water Bottles (0) | 2021.04.21 |
[LeetCode] - happy number (0) | 2021.04.20 |
[LeetCode] - Missing Number (0) | 2021.04.19 |