[프로그래머스] 해시 - 전화번호 목록(자바)

Rena·2022년 3월 29일
0

알고리즘 문제풀이

목록 보기
19/45
import java.util.HashMap;
import java.util.Map;

class Solution {
    public boolean solution(String[] phone_book) {
        boolean answer = true;
        Map<String,Integer> map = new HashMap<>();
        for(int i = 0; i<phone_book.length ; i++) {
            map.put(phone_book[i],i);
        }

        for(int i = 0; i < phone_book.length; i++) {
            for(int j = 0; j < phone_book[i].length();j++) {
                if(map.containsKey(phone_book[i].substring(0,j)))
                    return false;
            }
        }

        return answer;
    }

    public static void main(String[] args) {
        String[] array = {"23","14","2"};
        Solution slt = new Solution();
        System.out.println(slt.solution(array));
    }

}
profile
일을 사랑하고 싶은 개발자

0개의 댓글