문제
https://school.programmers.co.kr/learn/courses/30/lessons/72410?language=java
나의 풀이
class Solution {
public String solution(String new_id) {
new_id = new_id.toLowerCase();
new_id = new_id.replaceAll("[^a-z0-9-_.]", "");
if (new_id.length() != 0) {
char[] char_new_id = new_id.toCharArray();
StringBuilder sb = new StringBuilder();
boolean dotCheck = false;
for (char c : char_new_id) {
if (dotCheck && c == '.') {
continue;
}
dotCheck = c == '.' ? true : false;
sb.append(c);
}
new_id = sb.toString();
String firstStr = new_id.substring(0, 1);
String lastStr = new_id.substring(new_id.length() - 1, new_id.length());
if (firstStr.equals(".")) {
new_id = new_id.substring(1, new_id.length());
}
if (new_id.length() != 0 && lastStr.equals(".")) {
new_id = new_id.substring(0, new_id.length() - 1);
}
}
if (new_id.length() == 0) {
new_id = "a";
}
if (new_id.length() > 15) {
new_id = new_id.substring(0, new_id.substring(14, 15).equals(".") ? 14 : 15);
}
if (new_id.length() < 3) {
String lastStr2 = new_id.substring(new_id.length() - 1, new_id.length());
while (new_id.length() < 3) {
new_id += lastStr2;
}
}
return new_id;
}
}
O(n), 선형
'알고리즘 & 문제 > 프로그래머스' 카테고리의 다른 글
[프로그래머스] 3진법 뒤집기(java) (0) | 2023.02.20 |
---|---|
[프로그래머스] 내적(java) (0) | 2023.02.16 |
[프로그래머스] 음양 더하기(java) (0) | 2023.02.13 |
[프로그래머스] 둘만의 암호(java) (0) | 2023.02.10 |
[프로그래머스] 폰켓몬(java) (0) | 2023.02.09 |