๐Ÿ”ฅ[99ํด๋Ÿฝ ์ฝ”ํ…Œ ์Šคํ„ฐ๋””] 3์ผ์ฐจ TIL - ๋ฌธ์ž์—ด ๋‚ด ๋งˆ์Œ๋Œ€๋กœ ์ •๋ ฌํ•˜๊ธฐ

HOONSSACยท2024๋…„ 7์›” 24์ผ
1

99Club Coding Test Study

๋ชฉ๋ก ๋ณด๊ธฐ
3/41
post-thumbnail
post-custom-banner

โณ๋ฌธ์ œ

๋ฌธ์ œ ์„ค๋ช…

๋ฌธ์ž์—ด๋กœ ๊ตฌ์„ฑ๋œ ๋ฆฌ์ŠคํŠธ strings์™€, ์ •์ˆ˜ n์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ๊ฐ ๋ฌธ์ž์—ด์˜ ์ธ๋ฑ์Šค n๋ฒˆ์งธ ๊ธ€์ž๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•˜๋ ค ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด strings๊ฐ€ ["sun", "bed", "car"]์ด๊ณ  n์ด 1์ด๋ฉด ๊ฐ ๋‹จ์–ด์˜ ์ธ๋ฑ์Šค 1์˜ ๋ฌธ์ž "u", "e", "a"๋กœ strings๋ฅผ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค.

์ œํ•œ ์กฐ๊ฑด

  • strings๋Š” ๊ธธ์ด 1 ์ด์ƒ, 50์ดํ•˜์ธ ๋ฐฐ์—ด์ž…๋‹ˆ๋‹ค.
  • strings์˜ ์›์†Œ๋Š” ์†Œ๋ฌธ์ž ์•ŒํŒŒ๋ฒณ์œผ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.
  • strings์˜ ์›์†Œ๋Š” ๊ธธ์ด 1 ์ด์ƒ, 100์ดํ•˜์ธ ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค.
  • ๋ชจ๋“  strings์˜ ์›์†Œ์˜ ๊ธธ์ด๋Š” n๋ณด๋‹ค ํฝ๋‹ˆ๋‹ค.
  • ์ธ๋ฑ์Šค 1์˜ ๋ฌธ์ž๊ฐ€ ๊ฐ™์€ ๋ฌธ์ž์—ด์ด ์—ฌ๋Ÿฟ ์ผ ๊ฒฝ์šฐ, ์‚ฌ์ „์ˆœ์œผ๋กœ ์•ž์„  ๋ฌธ์ž์—ด์ด ์•ž์ชฝ์— ์œ„์น˜ํ•ฉ๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

stringsnreturn
["sun", "bed", "car"]1["car", "bed", "sun"]
["abce", "abcd", "cdx"]2["abcd", "abce", "cdx"]

์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช…

์ž…์ถœ๋ ฅ ์˜ˆ 1

"sun", "bed", "car"์˜ 1๋ฒˆ์งธ ์ธ๋ฑ์Šค ๊ฐ’์€ ๊ฐ๊ฐ "u", "e", "a" ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ๊ธฐ์ค€์œผ๋กœ strings๋ฅผ ์ •๋ ฌํ•˜๋ฉด ["car", "bed", "sun"] ์ž…๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ 2

"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) {
        String[] answer = new String[strings.length];
       
        strings = SWAP(strings, n);
        Arrays.sort(strings);
        strings = SWAP(strings, n);
        
        answer = strings;
        return answer;
    }
    
    public String[] SWAP(String[] strings, int n) {
        for (int i = 0; i < strings.length; i++) {
            String[] value = strings[i].split("");
            String tmp = value[0];
            value[0] = value[n];
            value[n] = tmp;
            
            strings[i] = String.join("", value);
        }
        return strings;    
    }
}

๋‚˜๋Š” ์ฒ˜์Œ์— n๋ฒˆ์งธ ์ธ๋ฑ์Šค์˜ ๋ฌธ์ž๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•˜๊ธฐ ์œ„ํ•ด์„œ ํ•ด๋‹น ๋ฌธ์ž์˜ ์ธ๋ฑ์Šค์™€ ์ฒซ ๋ฒˆ์งธ ์ธ๋ฑ์Šค์˜ ๋ฌธ์ž๋ฅผ ๋ฐ”๊พผ ํ›„ ์ •๋ ฌ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋  ๊ฒƒ์ด๋ผ ์ƒ๊ฐํ–ˆ๋‹ค.
๊ทธ๋ž˜์„œ ๋ฌด์ž‘์ • ๋ฌธ์ž์—ด์„ split()ํ•จ์ˆ˜๋กœ ๋‚˜๋ˆ„๊ณ , n๋ฒˆ์งธ ๋ฌธ์ž๋ฅผ ๊บผ๋‚ด๋Š” ๊ณผ์ •๊ณผ, ๊บผ๋‚ธ ๋ฌธ์ž์™€ ๋งจ ์•ž ์š”์†Œ์™€ ์ž๋ฆฌ๋ฅผ ๋ฐ”๊ฟ”์ค€ ํ›„ ๋‹ค์‹œ ๋ฌธ์ž๋“ค์„ join()ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ์žฌ์กฐ๋ฆฝ ์‹œํ‚ค๋Š” SWAPํ•จ์ˆ˜๋ผ๋Š” ๊ฒƒ์„ ๋งŒ๋“ค์–ด strings๋ฅผ ์ž…๋ ฅ๊ฐ’์œผ๋กœ ๋„ฃ์—ˆ๋‹ค.
๊ทธ๋Ÿฌ๋ฉด n๋ฒˆ์งธ ์ธ๋ฑ์Šค์˜ ๋ฌธ์ž์™€ ์ฒซ ๋ฒˆ์งธ ์ธ๋ฑ์Šค์˜ ๋ฌธ์ž๊ฐ€ ์„œ๋กœ ๋ฐ”๋€Œ๊ฒŒ ๋˜๊ณ , ์ด ์ƒํƒœ์—์„œ Arrays.sort()ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ์„ ํ•ด์ฃผ๋ฉด ๋œ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๋‹ค.

๋งˆ์ง€๋ง‰์œผ๋กœ, ๋ฐ”๋€ ๋ฌธ์ž ์ˆœ์„œ๋ฅผ ๋ณต๊ตฌ์‹œํ‚ค๊ธฐ ์œ„ํ•ด SWAPํ•จ์ˆ˜๋ฅผ ๋‹ค์‹œ ํ˜ธ์ถœํ•จ์œผ๋กœ์จ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ–ˆ๋‹ค๊ณ  ์ƒ๊ฐ์ด ๋“œ๋Š” ์ฐฐ๋‚˜..

ํ†ต๊ณผํ•œ ์ผ€์ด์Šค๋„ ์žˆ์—ˆ์ง€๋งŒ, ๋Œ€๋ถ€๋ถ„์˜ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์—์„œ ์‹คํŒจ๊ฐ€ ๋–ด๋‹ค.

์‹คํŒจ ์›์ธ

์•Œ๊ณ ๋ณด๋‹ˆ ์ œํ•œ ์กฐ๊ฑด ์ค‘ ๋งˆ์ง€๋ง‰ ์กฐ๊ฑด์„ ๊ฐ„๊ณผํ•œ ๊ฒƒ์ด์—ˆ๋‹ค!

์ธ๋ฑ์Šค 1์˜ ๋ฌธ์ž๊ฐ€ ๊ฐ™์€ ๋ฌธ์ž์—ด์ด ์—ฌ๋Ÿฟ ์ผ ๊ฒฝ์šฐ, ์‚ฌ์ „์ˆœ์œผ๋กœ ์•ž์„  ๋ฌธ์ž์—ด์ด ์•ž์ชฝ์— ์œ„์น˜ํ•ฉ๋‹ˆ๋‹ค.

๊ธฐ์กด์˜ ๋ฐฉ๋ฒ•๋Œ€๋กœ ๋ผ๋ฉด, n๋ฒˆ์งธ ์ธ๋ฑ์Šค์— ์œ„์น˜ํ•œ ๋ฌธ์ž๊ฐ€ ์„œ๋กœ ๊ฐ™์€ ๊ฒฝ์šฐ์™€ ์ƒ๊ด€ ์—†์ด ์ฒซ ๋ฒˆ์งธ ์ธ๋ฑ์Šค์™€ ์œ„์น˜๋ฅผ ๋ฐ”๊พผ ์ƒํƒœ์—์„œ ์ •๋ ฌ์„ ํ•˜์˜€๋Š”๋ฐ, ๊ทธ๋ ‡๊ฒŒ ๋˜๋ฉด ์ •๋ ฌ์˜ ๊ฒฐ๊ณผ๊ฐ€ ๋‹ฌ๋ผ์งˆ ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์„ ์ƒ๊ฐํ•˜์ง€ ๋ชปํ•œ ๊ฒƒ์ด๋‹ค.! ๊ทธ๋ž˜์„œ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์„ ์ฐพ์•„๋ณด๊ธฐ๋กœ ํ–ˆ๋‹ค.

๐Ÿ–ฅ๏ธ์ตœ์ข… ์ฝ”๋“œ

import java.util.*;

class Solution {
    public String[] solution(String[] strings, int n) {
        Arrays.sort(strings, (a, b) -> {
           if (a.charAt(n) != b.charAt(n)) {
               return Character.compare(a.charAt(n), b.charAt(n));
            }
            else {
                return a.compareTo(b);
            }
        });
        
        return strings;
    }
}

๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์„ ๊ณ ๋ฏผํ•˜๊ณ  ๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ํ’€์ด๋ฅผ ์ฐพ์•„๋ณด๋‹ค๊ฐ€ Arrays.sort()์˜ ๋‘ ๋ฒˆ์งธ ์ธ์ž๋กœ Comparator์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๋žŒ๋‹ค์‹์œผ๋กœ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ฐพ์•˜๋‹ค.

์ด ๋žŒ๋‹ค ์‹์€ ์ •๋ ฌ ๊ธฐ์ค€์„ ์ •์˜ํ•˜๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.
์ฆ‰, ๋‚ด๊ฐ€ ๊ตฌํ•˜๊ณ ์ž ํ•˜๋Š” ์ •๋ ฌ ๊ธฐ์ค€์„ ์—ฌ๊ธฐ๋‹ค๊ฐ€ ์ •์˜ํ•˜๋ฉด ๋˜๋Š” ๊ฒƒ์ด๋‹ค!

๊ทธ๋ž˜์„œ ์ •๋ ฌ ๊ธฐ์ค€์œผ๋กœ ๋‘ ๊ฐ€์ง€ ๊ฒฝ์šฐ๋ฅผ ๋‚˜๋ˆ„์–ด ์ฃผ์—ˆ๋‹ค.

  • n๋ฒˆ์งธ ๋ฌธ์ž๊ฐ€ ๋‹ค๋ฅผ ๊ฒฝ์šฐ
  • n๋ฒˆ์งธ ๋ฌธ์ž๊ฐ€ ๊ฐ™์„ ๊ฒฝ์šฐ

n๋ฒˆ์งธ ๋ฌธ์ž๊ฐ€ ๋‹ค๋ฅผ ๊ฒฝ์šฐ์—๋Š” ํ•ด๋‹น ์ธ๋ฑ์Šค์˜ ๋ฌธ์ž๋ฅผ charAt(n)ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ๊ตฌํ•œ ๋‹ค์Œ Character.compare()ํ•จ์ˆ˜์— ์ธ์ž๋กœ ๋„ฃ์–ด์ฃผ์—ˆ๋‹ค. ์—ฌ๊ธฐ์„œ Character.compare()ํ•จ์ˆ˜๋Š” ๋‘ ๊ฐœ์˜ ๋ฌธ์ž๋ฅผ ๋น„๊ตํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ํ•จ์ˆ˜์ด๋‹ค.
๊ทธ๋Ÿฌ๋ฏ€๋กœ a๊ฐ€ b๋ณด๋‹ค ํฌ๋ฉด ์–‘์ˆ˜ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๊ณ , ์ž‘์œผ๋ฉด ์Œ์ˆ˜ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๊ฒŒ ๋จ์œผ๋กœ์จ ๋‘ ๋ฌธ์ž์—ด์„ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•  ์ˆ˜ ์žˆ๋‹ค.

n๋ฒˆ์งธ ๋ฌธ์ž๊ฐ€ ๊ฐ™์„ ๊ฒฝ์šฐ์—๋Š” ๋‘ ๋ฌธ์ž์—ด์„ compareTo()ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ •๋ ฌ ์ˆœ์„œ๋ฅผ ๋ฐ˜ํ™˜ํ•ด์ฃผ๋ฉด ๋œ๋‹ค. Character.compare()ํ•จ์ˆ˜๋Š” ๋ฌธ์ž๋ฅผ ๋น„๊ตํ•˜์ง€๋งŒ, compareTo()ํ•จ์ˆ˜๋Š” ๋ฌธ์ž์—ด์„ ๋น„๊ตํ•˜๋Š” ํ•จ์ˆ˜์ด๋‹ค. ๋‹จ, ๋น„๊ต์— ์˜ํ•œ ๋ฐ˜ํ™˜๊ฐ’์€ ์„œ๋กœ ๋™์ผํ•˜๋‹ค.

์ด๋ ‡๊ฒŒ ํ•ด์„œ ๋ชจ๋“  ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋ฅผ ํ†ต๊ณผํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค!


๐Ÿ”—๋ฌธ์ œ ๋งํฌ
๐Ÿ’ปRepository

profile
ํ›ˆ์‹น์˜ ๊ฐœ๋ฐœ์—ฌํ–‰
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€