알고리즘 풀이/프로그래머스
[level2] 프로그래머스 - 전화번호 목록(JAVA)
데롱디롱
2021. 9. 14. 20:58
728x90
- 모든 전화번호를 Set에 넣어준다.
- 각 전화번호를 하나씩 잘라보면서 Set에 있는지 확인한다.
import java.util.*;
class Solution {
public boolean solution(String[] phone_book) {
Set<String> num = new HashSet<>();
num.addAll(Arrays.asList(phone_book));
for (String phone : phone_book) {
for(int i = 1; i < phone.length(); i++)
if(num.contains(phone.substring(0, i)))
return false;
}
return true;
}
}