https://programmers.co.kr/learn/courses/30/lessons/72410
์ฝ๋ฉํ ์คํธ ์ฐ์ต - ์ ๊ท ์์ด๋ ์ถ์ฒ
์นด์นด์ค์ ์ ์ฌํ ์ ์ ๊ฐ๋ฐ์ ๋ค์ค๋ "์นด์นด์ค๊ณ์ ๊ฐ๋ฐํ"์ ๋ฐฐ์น๋์ด, ์นด์นด์ค ์๋น์ค์ ๊ฐ์ ํ๋ ์ ์ ๋ค์ ์์ด๋๋ฅผ ์์ฑํ๋ ์ ๋ฌด๋ฅผ ๋ด๋นํ๊ฒ ๋์์ต๋๋ค. "๋ค์ค"์๊ฒ ์ฃผ์ด์ง ์ฒซ ์ ๋ฌด๋ ์๋ก
programmers.co.kr
2021 KAKAO BLIND RECRUITMENT์์ ์ถ์ ๋ ๋ฌธ์ ์ธ "์ ๊ท ์์ด๋ ์ถ์ฒ" ๋ฌธ์ ํ์ด์ด๋ค.
ํ๋ก๊ทธ๋๋จธ์ค๋ผ๋ ์ฝ๋ฉํ ์คํธ ๋ฌธ์ ํ์ด ํ๋ซํผ์์ ์นด์นด์ค์์ ์ถ์ ๋๋ ์ฝ๋ฉํ ์คํธ ๋ฌธ์ ๋ค์ ํ์ธํด๋ณด๊ณ ํ์ด๋ณผ ์ ์๋ค.
์ฌ์ค ์ด ๋ฌธ์ ๋ ์๊ณ ๋ฆฌ์ฆ์ ์๊ฐํด๋ด๋ ๋ถ๋ถ์ ๊ฑฐ์ ์๋ค๊ณ ํ ์ ๋๋ก ๋ฌธ์ ์ ๋์์๋ ์ค๋ช ์ ๋ฐ๋ฅด๋ฉด ๋๋ ๋ฌธ์ ์๋ค.
์ ๊ท์์ ์ฌ์ฉํ์ง ์์๋ ๋์ง๋ง, ์ฌ์ฉํ๋ฉด ์๋์ ๊ฐ์ด ๊ฐ๋จํ ํ์ด๊ฐ ๊ฐ๋ฅํ๋ค.
import java.util.*; class Solution { public String solution(String new_id) { String tempId = new_id; String regExp = ""; // 1๋จ๊ณ new_id์ ๋ชจ๋ ๋๋ฌธ์๋ฅผ ๋์๋๋ ์๋ฌธ์๋ก ์นํํฉ๋๋ค. tempId = tempId.toLowerCase(); // 2๋จ๊ณ new_id์์ ์ํ๋ฒณ ์๋ฌธ์, ์ซ์, ๋นผ๊ธฐ(-), ๋ฐ์ค(_), ๋ง์นจํ(.)๋ฅผ ์ ์ธํ ๋ชจ๋ ๋ฌธ์๋ฅผ ์ ๊ฑฐํฉ๋๋ค. regExp = "[^a-z0-9-_.]"; tempId = tempId.replaceAll(regExp, ""); // 3๋จ๊ณ new_id์์ ๋ง์นจํ(.)๊ฐ 2๋ฒ ์ด์ ์ฐ์๋ ๋ถ๋ถ์ ํ๋์ ๋ง์นจํ(.)๋ก ์นํํฉ๋๋ค. regExp = "\\.{2,}"; tempId = tempId.replaceAll(regExp, "."); // 4๋จ๊ณ new_id์์ ๋ง์นจํ(.)๊ฐ ์ฒ์์ด๋ ๋์ ์์นํ๋ค๋ฉด ์ ๊ฑฐํฉ๋๋ค. regExp = "^[.]|[.]$"; tempId = tempId.replaceAll(regExp, ""); // 5๋จ๊ณ new_id๊ฐ ๋น ๋ฌธ์์ด์ด๋ผ๋ฉด, new_id์ "a"๋ฅผ ๋์
ํฉ๋๋ค. if (tempId.length() == 0) { tempId = "a"; } // 6๋จ๊ณ new_id์ ๊ธธ์ด๊ฐ 16์ ์ด์์ด๋ฉด, new_id์ ์ฒซ 15๊ฐ์ ๋ฌธ์๋ฅผ ์ ์ธํ ๋๋จธ์ง ๋ฌธ์๋ค์ ๋ชจ๋ ์ ๊ฑฐํฉ๋๋ค. // ๋ง์ฝ ์ ๊ฑฐ ํ ๋ง์นจํ(.)๊ฐ new_id์ ๋์ ์์นํ๋ค๋ฉด ๋์ ์์นํ ๋ง์นจํ(.) ๋ฌธ์๋ฅผ ์ ๊ฑฐํฉ๋๋ค. if (tempId.length() >= 16) { tempId = tempId.substring(0, 15); if (tempId.charAt(tempId.length() - 1) == '.') { tempId = tempId.substring(0, tempId.length() - 1); } } // 7๋จ๊ณ new_id์ ๊ธธ์ด๊ฐ 2์ ์ดํ๋ผ๋ฉด, new_id์ ๋ง์ง๋ง ๋ฌธ์๋ฅผ new_id์ ๊ธธ์ด๊ฐ 3์ด ๋ ๋๊น์ง ๋ฐ๋ณตํด์ ๋์ ๋ถ์
๋๋ค. if (tempId.length() <= 2) { int len = tempId.length(); char lastChar = tempId.charAt(tempId.length() - 1); for (int i = 0; i < 3-len; i++) { tempId += lastChar; } } return tempId; } }
'๊ฐ๋ฐ > ์๋ฃ๊ตฌ์กฐ & ์๊ณ ๋ฆฌ์ฆ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์คํ์ฑํ ๋ฐฉ (Java) (0) | 2021.10.24 |
---|---|
๋ก๋์ ์ต๊ณ ์์์ ์ต์ ์์ (Java) (0) | 2021.10.24 |
์ฐฝ๊ณ ๋ค๊ฐํ (Java) (0) | 2021.10.21 |
์ค์ฒฉ ๋ฐ๋ณต๋ฌธ ๋์ฒดํ๊ธฐ (0) | 2021.10.14 |
1๋ถํฐ n๊น์ง์ ํฉ์ ๋ฐํํ๋ ์๊ณ ๋ฆฌ์ฆ (0) | 2021.10.13 |
๋๊ธ