Java

[프로그래머스] 튜플

프로버티기 2025. 4. 12. 16:57

문제

 

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

 

프로그래머스

SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

 

 

입출력

 

 


아이디어

- 앞 뒤 {{ }} 제거 

- 정규 표현식 작성

- 이미 앞에서 있던 값은 제외 

 

 

 

코드 

 

import java.util.*;

class Solution {
    public int[] solution(String s) {
        s = s.substring(2, s.length() - 2); // 앞 "{{", 뒤 "}}" 제거
        String[] groups = s.split("\\},\\{"); // "},{" 기준으로 나누기

        List<String[]> all = new ArrayList<>();
        for (String group : groups) {
            all.add(group.split(","));
        }

        all.sort(Comparator.comparingInt(a -> a.length));

        Set<Integer> seen = new HashSet<>();
        List<Integer> result = new ArrayList<>();

        for (String[] group : all) {
            for (String numStr : group) {
                int num = Integer.parseInt(numStr);
                if (!seen.contains(num)) {
                    seen.add(num);
                    result.add(num);
                }
            }
        }

        return result.stream().mapToInt(i -> i).toArray();
    }
}

'Java' 카테고리의 다른 글

[codetree] 언덕 깎기  (0) 2025.05.18
[프로그래머스] 호텔 대실  (0) 2025.04.12
[백준] 3020 : 개똥벌레  (0) 2025.04.06
[백준] 2098 : 외판원 순회  (0) 2025.04.06
[프로그래머스] 후보키  (0) 2025.04.03