๋ฐ์ํ
๐ ๋ฌธ์ URL ๐
class Solution {
public String solution(String new_id) {
String answer = "";
//1๋จ๊ณ
new_id = new_id.toLowerCase();
System.out.println("1๋จ๊ณ :"+new_id);
//2๋จ๊ณ
StringBuffer sb = new StringBuffer();
for(int i=0; i<new_id.length(); i++) {
if (String.valueOf(new_id.charAt(i)).matches("[0-9a-z._-]")) {
sb.append(new_id.charAt(i));
}
}
new_id = sb.toString();
System.out.println("2๋จ๊ณ :"+new_id);
//3๋จ๊ณ
StringBuffer sb2 = new StringBuffer();
int cnt = 0;
for(int i=0; i<new_id.length(); i++) {
if (new_id.charAt(i) == '.') {
cnt++;
if (cnt < 2) {
sb2.append(new_id.charAt(i));
}
} else {
cnt = 0;
sb2.append(new_id.charAt(i));
}
}
new_id = sb2.toString();
System.out.println("3๋จ๊ณ :"+new_id);
if(new_id.charAt(0) == '.'){
new_id = new_id.substring(1);
}else if(new_id.charAt(new_id.length()-1) == '.'){
new_id = new_id.substring(0, new_id.length()-1);
}
System.out.println("4๋จ๊ณ :"+new_id);
if(new_id.length() == 0){
new_id = "a";
}
System.out.println("5๋จ๊ณ :"+new_id);
if(new_id.length() >= 16){
new_id = new_id.substring(0, 15);
if(new_id.charAt(new_id.length()-1) == '.'){
new_id = new_id.substring(0, new_id.length()-1);
}
}
System.out.println("6๋จ๊ณ :"+new_id);
if(new_id.length() ==2 ){
new_id = new_id + new_id.charAt(1);
}else if(new_id.length() ==1){
new_id = new_id + new_id.charAt(0) + new_id.charAt(0);
}
System.out.println("7๋จ๊ณ :"+new_id);
answer = new_id;
return answer;
}
}
๊ฒฐ๊ณผ..
2๋จ๊ณ๋ง ์ ๊ท์์ผ๋ก ํ์๋๋ฐ.. ์ฐพ์๋ณด๋ 3,4,5 ๋จ๊ณ๋ ์ ๊ท์์ผ๋ก ํ์์ด์ผ ํ๋ค..
2๋จ๊ณ๋ ์ข ๋ ๊ฐ๋จํ๊ฒ ํํ์ด ๊ฐ๋ฅํ๋ค.
new_id = new_id.replaceAll("[^0-9a-z._-]", "");
[]์์์ ^๋ ๊ทธ ๋ฌธ์๊ฐ ์๋๊ฒ๋ค์ ์๊ธฐํ๋ค.
3๋จ๊ณ๋ ์๋์ ๊ฐ์ด ์์ ํ์๋ค.
new_id = new_id.replaceAll("[.]{2,}", ".");
[a]{2}์ด๋ฉด aa์ด๊ณ [a]{2,}์ด๋ฉด a๊ฐ 2๊ฐ ์ด์์ด๊ณ , [a]{2, 4}์ด๋ฉด aa, aaa, aaaa ์ด๋ค.
4๋จ๊ณ๋ ์๋์ ๊ฐ์ด ์์ ํ์๋ค.
new_id = new_id.replaceAll("^[.]|[.]$", "");
^ : ์์ํ์, $ : ๋ํ์
.์ผ๋ก ์์ํ๊ฑฐ๋ .์ผ๋ก ๋๋๋ฉด ""๋ก ์นํํ๋ค.
6๋จ๊ณ์์๋ ๋ง์ง๋ง์ด .์ผ๋ก ๋๋๋์ง ์ฒดํฌํ ๋ ์ ๊ท์์ ์ฌ์ฉํ๋ ๊ฒ์ผ๋ก ์์ ํ์๋ค.
new_id = new_id.replaceAll("[.]$","");
์ต์ข ์์ค
class Solution {
public String solution(String new_id) {
String answer = "";
//1๋จ๊ณ
new_id = new_id.toLowerCase();
//2๋จ๊ณ
new_id = new_id.replaceAll("[^0-9a-z._-]", "");
//3๋จ๊ณ
new_id = new_id.replaceAll("[.]{2,}", ".");
//4๋จ๊ณ
new_id = new_id.replaceAll("^[.]|[.]$", "");
//5๋จ๊ณ
if(new_id.length() == 0){
new_id = "a";
}
//6๋จ๊ณ
if(new_id.length() >= 16){
new_id = new_id.substring(0, 15);
new_id = new_id.replaceAll("[.]$","");
}
//7๋จ๊ณ
if(new_id.length() ==2 ){
new_id = new_id + new_id.charAt(1);
}else if(new_id.length() ==1){
new_id = new_id + new_id.charAt(0) + new_id.charAt(0);
}
answer = new_id;
return answer;
}
}
๋ค ํต๊ณผํ๋ค..! ์ ๊ท์๊ณผ๋ ์ข ๊ฐ๊น์์ง ํ์๊ฐ...์๋ค....
728x90
๋ฐ์ํ
'๊ฐ๋ฐ > ์๊ณ ๋ฆฌ์ฆ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค/JAVA] ํคํจ๋ ๋๋ฅด๊ธฐ (0) | 2022.07.25 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค/JAVA] ์ซ์ ๋ฌธ์์ด๊ณผ ์๋จ์ด (0) | 2022.07.24 |
[ํ๋ก๊ทธ๋๋จธ์ค/JAVA] ๋ก๋์ ์ต๊ณ ์์์ ์ต์ ์์ (0) | 2022.07.24 |
[ํ๋ก๊ทธ๋๋จธ์ค/JAVA] ์ ๊ณ ๊ฒฐ๊ณผ ๋ฐ๊ธฐ (1) | 2022.07.24 |
[ํ๋ก๊ทธ๋๋จธ์ค/JAVA] ๊ฐ์ฅ ํฐ ์ - ์ ๋ ฌ (0) | 2022.02.20 |
๋๊ธ