https://school.programmers.co.kr/learn/courses/30/lessons/42577
프로그래머스
SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
Trie 자료구조 isEnd 로 확인하기
import java.util.*;
class Solution {
public boolean solution(String[] phone_book) {
for (String phone : phone_book) {
if (!insert(phone)) {
return false;
}
}
return true;
}
static class TrieNode {
private boolean isEnd;
private Map<Character, TrieNode> children = new HashMap<>();
}
static TrieNode root = new TrieNode();
static boolean insert(String phone) {
TrieNode current = root;
for (char c : phone.toCharArray()) {
current.children.putIfAbsent(c, new TrieNode());
current = current.children.get(c);
if (current.isEnd) { // 번호 중간에서 이미 끝인 경우
return false;
}
}
current.isEnd = true;
return current.children.isEmpty();
}
}
[프로그래머스] k진수에서 소수 개수 구하기 (0) | 2025.02.01 |
---|---|
[프로그래머스] [1차] 뉴스 클러스터링 (0) | 2025.02.01 |
[프로그래머스] 광물 캐기 (0) | 2025.01.31 |
[프로그래머스] 피로도 (0) | 2025.01.31 |
[프로그래머스] 붕대감기 (0) | 2025.01.31 |