코딩테스트

[프로그래머스/Java] 전화번호 목록 (HashMap)

데메즈 2026. 7. 6. 07:48
반응형

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

 

 

map.containsKey(phone_book[i].substring(0, j))

import java.util.HashMap;

class Solution {
    public boolean solution(String[] phone_book) {
        boolean answer = true;
        
        HashMap<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;
    }
}
반응형