https://programmers.co.kr/learn/courses/30/lessons/12915
[ ๋ฌธ์ ]
๋ฌธ์์ด๋ก ๊ตฌ์ฑ๋ ๋ฆฌ์คํธ strings์, ์ ์ n์ด ์ฃผ์ด์ก์ ๋, ๊ฐ ๋ฌธ์์ด์ ์ธ๋ฑ์ค n๋ฒ์งธ ๊ธ์๋ฅผ ๊ธฐ์ค์ผ๋ก ์ค๋ฆ์ฐจ์ ์ ๋ ฌํ๋ ค ํฉ๋๋ค. ์๋ฅผ ๋ค์ด strings๊ฐ ["sun", "bed", "car"]์ด๊ณ n์ด 1์ด๋ฉด ๊ฐ ๋จ์ด์ ์ธ๋ฑ์ค 1์ ๋ฌธ์ "u", "e", "a"๋ก strings๋ฅผ ์ ๋ ฌํฉ๋๋ค.
[ ์ ํ์กฐ๊ฑด ]
- strings๋ ๊ธธ์ด 1 ์ด์, 50์ดํ์ธ ๋ฐฐ์ด์ ๋๋ค.
- strings์ ์์๋ ์๋ฌธ์ ์ํ๋ฒณ์ผ๋ก ์ด๋ฃจ์ด์ ธ ์์ต๋๋ค.
- strings์ ์์๋ ๊ธธ์ด 1 ์ด์, 100์ดํ์ธ ๋ฌธ์์ด์ ๋๋ค.
- ๋ชจ๋ strings์ ์์์ ๊ธธ์ด๋ n๋ณด๋ค ํฝ๋๋ค.
- ์ธ๋ฑ์ค 1์ ๋ฌธ์๊ฐ ๊ฐ์ ๋ฌธ์์ด์ด ์ฌ๋ฟ ์ผ ๊ฒฝ์ฐ, ์ฌ์ ์์ผ๋ก ์์ ๋ฌธ์์ด์ด ์์ชฝ์ ์์นํฉ๋๋ค.
[ ์ ์ถ๋ ฅ ์ ]
strings | n | return |
["sun", "bed", "car"] | 1 | ["car", "bed", "sun"] |
["abce", "abcd", "cdx"] | 2 | ["abcd", "abce", "cdx"] |
์ ์ถ๋ ฅ ์ ์ค๋ช
Ex1)
"sun", "bed", "car"์ 1๋ฒ์งธ ์ธ๋ฑ์ค ๊ฐ์ ๊ฐ๊ฐ u, e, a ์
๋๋ค. ์ด๋ฅผ ๊ธฐ์ค์ผ๋ก strings๋ฅผ ์ ๋ ฌํ๋ฉด ["car", "bed", "sun"] ์
๋๋ค.
Ex2)
"abce", "abcd", "cdx"์ 2๋ฒ์งธ ์ธ๋ฑ์ค ๊ฐ์ "c", "c", "x"์
๋๋ค. ๋ฐ๋ผ์ ์ ๋ ฌ ํ์๋ "cdx"๊ฐ ๊ฐ์ฅ ๋ค์ ์์นํฉ๋๋ค. "abce"์ "abcd"๋ ์ฌ์ ์์ผ๋ก ์ ๋ ฌํ๋ฉด "abcd"๊ฐ ์ฐ์ ํ๋ฏ๋ก, ๋ต์ ["abcd", "abce", "cdx"] ์
๋๋ค.
[ ํ์ด ]
import java.util.*;
class Solution {
public String[] solution(String[] strings, int n) {
Arrays.sort(strings, new Comparator<String>() {
@Override
public int compare(String s1, String s2){
char c1 = s1.charAt(n);
char c2 = s2.charAt(n);
// n๋ฒ์งธ ๋ฌธ์๊ฐ ๊ฐ์ ๊ฒฝ์ฐ์ ์๋ณธ ์คํธ๋ง์ ์ฌ์ ์์ผ๋ก
if(c1 == c2){
return s1.compareTo(s2);
} else return c1 - c2;
}
});
return strings;
}
}