Home [프로그래머스] 전화번호 목록
Post
Cancel

[프로그래머스] 전화번호 목록

[프로그래머스] 전화번호 목록

https://programmers.co.kr/learn/courses/30/lessons/42577

풀이방법

  • 효율성을 위해 정렬을 먼저 수행
  • “12”, “132”, “125”, “1234”를 정렬하면 “12”, “1234”, “125”, “132”가 되므로 다음 위치의 값이 현재 위치의 값으로 시작하게 될 경우 false 반환
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import java.util.Arrays;

class Solution {
    public boolean solution(String[] phone_book) {
        Arrays.sort(phone_book);

        for (int i=0; i<phone_book.length-1; i++) {
            if (phone_book[i+1].indexOf(phone_book[i]) == 0) {
                return false;
            }
        }

        return true;
    }
}

시행착오

  • 처음에는 무난하게 2중 for문으로 작성했으나 효율성에서 탈락했다.
This post is licensed under CC BY 4.0 by the author.

[프로그래머스] 완주하지 못한 선수

[BOJ 2468] 안전 영역

Comments powered by Disqus.