License Key Formatting
You are given a license key represented as a string S which only alphanumeric character and dashes. The string is separated into N+1 groups by N dashes.
Given a number K, we would want to reformat the strings such that each group contains exactly K characters, except for the first group which could be shorter than K, but still must contain at least one character. Furthermore, there must be a dash inserted between two groups and all lowercase letters should be converted to uppercase.
Given a non-empty string S and a number K, format the string according to the rules described above.
=>์ค์ง ์ํ๋ฒณ ์ฒ ์์ ๋์ฌ๋ก ์ด๋ฃจ์ด์ง String S๋ก ๋ํ๋ด์ง๋ ๋ผ์ด์ผ์ค ํค๊ฐ ์ฃผ์ด์ง๋ค. String์ N๊ฐ์ ๋์ฌ์ ์ํด N+1 ๊ทธ๋ฃน์ผ๋ก ๋ถ๋ฆฌ๋๋ค.
์ซ์ K๊ฐ ์ฃผ์ด์ง๋๋ฐ, ์ฐ๋ฆฌ๋ string์ ์ฌํ์ฑํ๊ณ ์ ํ๋ค. ๊ฐ๊ฐ์ ๊ทธ๋ฃน์ ์ ํํ K๊ฐ์ character๋ฅผ ํฌํจํ๊ณ , K๋ณด๋ค ์์ ์๋ ์๋ ์ฒซ๋ฒ์งธ ๊ทธ๋ฃน์ ์ ์ธํ๊ณ ์ต์ํ 1๊ฐ์ character๋ฅผ ํฌํจํด์ผํ๋ค. ๊ฒ๋ค๊ฐ, ๋ ๊ฐ์ ๊ทธ๋ฃน ์ฌ์ด์ ๋์ฌ๊ฐ ์ฝ์ ๋์ด์ผ ํ๊ณ ๋ชจ๋ ์๋ฌธ์๋ ๋๋ฌธ์๋ก ๋ณํ๋์ด์ผ ํ๋ค.
The length of string S will not exceed 12,000, and K is a positive integer.
String S consists only of alphanumerical characters (a-z and/or A-Z and/or 0-9) and dashes(-).
String S is non-empty.
Example 1 : Input : S = "5F3Z-2e-9-2", K = 4 Output : "5F3Z-2E92"
Example 2 : Input : S = "2-5g-3-J",K = 2 Output : "2-5G-3J"
์ฌ์ฉํ ์๋ฃ๊ตฌ์กฐ : StringBuilder =>String : String์ +์ฐ์ฐ์ด๋ concat์ ์ฌ์ฉํ๋ฉด ์๋ก์ด String ๊ฐ์ฒด๋ฅผ new๋ก ๋ง๋ค๊ธฐ ๋๋ฌธ์ ๊ทธ๋งํผ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ๋ง์ด ์ฐจ์งํ๊ธฐ ๋๋ฌธ์ด๋ค. =>StringBuffer : ๋ฉํฐ์ค๋ ๋ ํ๊ฒฝ์์ ๋๊ธฐํ(Synchronized) =>StringBuilder : ์ฑ๊ธ์ค๋ ๋ ํ๊ฒฝ์์ ๋น๋๊ธฐํ(Asynchronized). Append ๋ฑ ์ฐ์ฐ์ ์์ ์์ฌ๋ก ์ฐ๊ธฐ ์ํด ์ฌ์ฉํ๋ค.
์๊ณ ๋ฆฌ์ฆ 1. ์๋ String์์ ๋์ฌ(-)์ ๊ฑฐํ๋ค. - replace(char oldChar, char newChar) 2. ์๋ฌธ์๋ ๋๋ฌธ์๋ก ๋ฐ๊พผ๋ค. - toUpperCase 3. ๋ค์์๋ถํฐ ์นด์ดํ ํ๋ค! - for(int i=k; i < leng; i = i+k)
=>๋ ์ง๊ด์ ์ผ๋ก
์๋ก์ด String ๊ฐ์ฒด๋ฅผ ์์ฑํ์ฌ input String์์ ๋์ฌ ์ ๊ฑฐ, ๋๋ฌธ์ ๋ณํํ๋ค.
StringBuilder ์์ฑํ์ฌ 1์์ ์์ฑํ String ๋ฐ์ดํฐ ์ถ๊ฐํ๋ค.
StringBuilder์ ๋ค์์๋ถํฐ K๋งํผ ๋์ด ๋์ฌ ์ถ๊ฐํ๋ค. //insert(int,'-');
StringBuilder๋ฅผ String์ผ๋ก ๋ณํํ์ฌ ๋ฆฌํดํ๋ค.
์๊ณ ๋ฆฌ์ฆ์ java ์ธ์ด๋ก ๊ตฌํ #1.(ํ๋ฆผ)
์๊ณ ๋ฆฌ์ฆ์ java ์ธ์ด๋ก ๊ตฌํ #2.(์ ๋ต)
์ ๋ต :
Last updated
Was this helpful?