Home
Posts
Categories
Series
Tags
About
tuple
Lv2
postedOn: 2024-5-1   updatedOn: 2024-5-1   includedIn: Lv2
wordsCount: 540   readingTime: 3 mins   viewers:

Problem

A “tuple” is defined as an ordered collection of elements which can include duplicate values and each element is assigned an order. A tuple with n elements (a1, a2, a3, ..., an) can be expressed using the set notation in various ways, with any ordering of the subsets as:

{{a1}, {a1, a2}, {a1, a2, a3}, ..., {a1, a2, a3, ..., an}}

For example, the tuple (2, 1, 3, 4) can be represented as:

{{2}, {2, 1}, {2, 1, 3}, {2, 1, 3, 4}}

The sets can be permuted, but they still represent the same tuple:

{{2, 1, 3, 4}, {2}, {2, 1, 3}, {2, 1}}
{{1, 2, 3}, {2, 1}, {1, 2, 4, 3}, {2}}

Given a string s that represents a tuple in the form of a set of sets, you need to determine the elements of the tuple in their correct order. The string s is formatted with numbers and the characters {, }, and ,.

Constraints

  • The length of s is between 5 and 1,000,000.
  • s consists of numbers, {, }, and ,. No number starts with 0.
  • s always correctly represents a tuple with no duplicate elements.
  • The elements of the tuple are natural numbers between 1 and 100,000.
  • The output array will have between 1 and 500 elements.

Example Inputs and Outputs

s result
“{{2},{2,1},{2,1,3},{2,1,3,4}}” [2, 1, 3, 4]
“{{1,2,3},{2,1},{1,2,4,3},{2}}” [2, 1, 3, 4]
“{{20,111},{111}}” [111, 20]
“{{123}}” [123]
“{{4,2,3},{3},{2,3,4,1},{2,3}}” [3, 2, 4, 1]

Explanation of Examples

  • Example 1: The order in the sets varies but consistently starts from a smaller set to the full set representing the tuple. The tuple (2, 1, 3, 4) is formed by determining which elements appear as the sets grow.
  • Example 2: Although the sets are given in a different order, they still indicate the same tuple as example 1.
  • Example 3: (111, 20) is represented, and while the order in the set might suggest (20, 111), the tuple format ensures that 111 appears first since it’s isolated in a set by itself initially.
  • Example 4: A single-element tuple is directly indicated.
  • Example 5: By analyzing the growth of sets from smaller to larger, it can be discerned that the tuple is (3, 2, 4, 1).

The challenge involves parsing the string to extract sets, then determining the tuple order by the size of these sets and the introduction of new elements in each subsequent set.


How to Approach?

find one element tuple -> transform to integer and remove from total

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import java.util.*;

class Solution {
    public int[] solution(String s) {
        // Remove the outermost braces and split the string by "},{"
        String[] tuples = s.substring(2, s.length() - 2).split("\\},\\{");
        Arrays.sort(tuples, (a, b) -> a.length() - b.length()); // Sort by length
        
        Set<Integer> seen = new HashSet<>();
        List<Integer> result = new ArrayList<>();
        
        // Process each group of numbers
        for (String tuple : tuples) {
            String[] nums = tuple.split(",");
            for (String num : nums) {
                int value = Integer.parseInt(num.trim());
                if (seen.add(value)) { // Add only if not already added
                    result.add(value);
                }
            }
        }
        
        // Convert List<Integer> to int[]
        return result.stream().mapToInt(i -> i).toArray();
    }
}
Aaron Oh
고려대코딩개발협동조합 창단 멤버
Table of Contents
Related Posts
braket rotating
Problem Define a string as a “valid bracket string” if it follows the given rules: “()”, “[]”, and “{}” are all valid bracket strings. If A is a valid bracket string, then “(A)”, “[A]”, and “{A}” are also valid bracket strings. For example, since “[]” is a valid bracket string, “([])” is also valid.
2024-4-27
Tournament
Problem In the Jay’s game tournament, N players compete in a knockout format. Each player is sequentially assigned a number from 1 to N. The matches are arranged as 1 vs 2, 3 vs 4, …, N-1 vs N. The winner of each game advances to the next round, where they receive new sequential numbers starting from 1 for the advancing players.
2024-4-25
Last and First
Problem Several people are playing the game of English word chain where each player takes turns saying a word that must begin with the last letter of the previous word. Here are the rules: Players take turns in a sequence from 1 to n. After the last player, it starts again with the first player.
2024-4-23
High Score Kit: Stack&Queue#2
Problem The Programmers team is currently performing feature improvement tasks. Each feature can only be released when its progress reaches 100%. However, since the development speed of each feature varies, it is possible for features developed later to be completed earlier. In such cases, a feature that is developed later will be released together with the feature in front of it that is scheduled for earlier release.
2024-4-10
High Score Kit: Stack&Queue#1
Problem Given an array arr consisting of integers ranging from 0 to 9, the task is to remove consecutive duplicates while preserving the order of the remaining elements. For instance, if arr = [1, 1, 3, 3, 0, 1, 1], the result should be [1, 3, 0, 1]. Similarly, for arr = [4, 4, 4, 3, 3], the result should be [4, 3].
2024-4-10
High Score Kit: Greedy#1
Problem Given a number, your task is to remove k digits from the number in such a way that the remaining digits form the largest possible number. The number is provided as a string number and the integer k represents the number of digits to remove. Your function solution should return the resulting largest number as a string.
2024-4-11