package com.company;
import java.util.*;
public class Solution {
static public void main(String[] args) {
String quiz1 = "{{2},{2,1},{2,1,3},{2,1,3,4}}" ;
String quiz2 = "{{1,2,3},{2,1},{1,2,4,3},{2}}" ;
String quiz3 = "{{20,111},{111}}";
String quiz4 = "{{123}}";
String quiz5 = "{{4,2,3},{3},{2,3,4,1},{2,3}}";
System.out.println(Arrays.toString(solution(quiz1)));
System.out.println(Arrays.toString(solution(quiz2)));
System.out.println(Arrays.toString(solution(quiz3)));
System.out.println(Arrays.toString(solution(quiz4)));
System.out.println(Arrays.toString(solution(quiz5)));
}
static public Integer[] solution(String s) {
Integer[] answer = {};
Set<Integer> set = new LinkedHashSet<>();
String deletedBracketString = getDeletedBracketString(s);
String[] strArray = deletedBracketString.replace("},{"," ").split(" ");
Arrays.sort(strArray,new MyComparator());
for (String str : strArray) {
for(String str2: str.split(",")){
set.add(Integer.parseInt(str2));
}
}
return set.toArray(answer);
}
private static String getDeletedBracketString(String str) {
return str.substring(2,str.length()-2);
}
}
class MyComparator implements Comparator<String> {
@Override
public int compare(String o1, String o2) {
if(o1.length() < o2.length())
return -1;
return 1;
}
}