Publish:

ํƒœ๊ทธ: , , , , ,

์นดํ…Œ๊ณ ๋ฆฌ:

img_3.png

๋ฌธ์ œ

๋ฌธ์ œ ๋งํฌ

img_3.png

ํ’€์ด (O(n log n))

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
  public boolean solution(String[] phone_book) {
    boolean answer = true;
    Arrays.sort(phone_book);
    for (int i = 0; i < phone_book.length - 1; i++) {
      if (phone_book[i + 1].startsWith(phone_book[i])) {
        answer = false;
        break;
      }
    }
    return answer;
  }
}

๋ฌธ์ œ ๋ถ„๋ฅ˜๋Š” ํ•ด์‹œ๋ผ๊ณ  ๋‚˜์˜ค๋Š”๋ฐ.. ํ•ด์‹œ๋ณด๋‹จ ๋ฐฐ์—ด์„ ์ •๋ ฌํ•ด ๋ฌธ์ž์—ด ๋น„๊ต๋กœ ํ‘ธ๋Š” ๊ฒƒ์ด ํ›จ์”ฌ ํšจ์œจ์ ์œผ๋กœ ๋ณด์ธ๋‹ค. ์ด ํ’€์ด ๋ฐฉ๋ฒ•์˜ ํ•ต์‹ฌ์€ Arrays.sort() ๋กœ ์ดˆ๊ธฐ ๋ฐฐ์—ด์„ ์šฐ์„  ์ •๋ ฌ์„ ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

๋งŒ์•ฝ ์ •๋ ฌ์„ ํ•˜์ง€ ์•Š๊ณ  ์ด์ค‘ for ๋ฌธ์œผ๋กœ ํ’€๊ฒŒ๋˜๋ฉด ๋ชจ๋“  ๋ฌธ์ž์—ด์„ ๋น„๊ตํ•  ์ˆ˜ ๋ฐ–์— ์—†์–ด์„œ O(n^2) ์˜ ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ ๋œ๋‹ค. ๋˜ํ•œ String[] arr4 = {"123", "12"}; ์ด๋Ÿฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ์ฒ˜๋Ÿผ ์ ‘๋‘์‚ฌ๊ฐ€ ๋’ค์ชฝ์— ์žˆ๋Š” ๊ฒฝ์šฐ์—๋„ ์ž˜๋ชป๋œ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜จ๋‹ค.

๋ฐฉ๋ฌธํ•ด ์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค! ๋Œ“๊ธ€,์ง€์ ,ํ”ผ๋“œ๋ฐฑ ์–ธ์ œ๋‚˜ ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค๐Ÿ˜Š

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ