(Hacker Rank) Secrete Message Groups - Solution

Link to the problem: https://www.hackerrank.com/contests/womens-codesprint/challenges/grouping-the-items

After counting the amount of each digit for each number, we group them in by the frequency of 0-9. Then, the amount of groups is printed, followed by the groups that have the largest size.


import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int numStrings = sc.nextInt();
        String[] strings = new String[numStrings];
        for (int i = 0; i < numStrings; i++) {
            strings[i] = sc.next();
        }
       
        TreeMap<String, TreeMap<Integer, Integer>> map = new TreeMap<String, TreeMap<Integer, Integer>>();
        for (int i = 0; i < numStrings; i++) {
            map.put(strings[i], new TreeMap<Integer, Integer>());
            TreeMap<Integer, Integer> list = map.get(strings[i]);
            for (int j = 0; j < strings[i].length(); j++) {
                if (!list.containsKey(strings[i].charAt(j)-'0')) {
                    list.put(strings[i].charAt(j)-'0', 1);
                }
                else {
                    list.put(strings[i].charAt(j)-'0', list.get(strings[i].charAt(j)-'0')+1);
                }
            }
        }
       
        int index = 0;
        TreeSet<String> considered = new TreeSet<String>();
        ArrayList<ArrayList<String>> groups = new ArrayList<ArrayList<String>>();
        for (int i = 0; i < numStrings; i++) {
            if (considered.contains(strings[i])) {
                continue;
            }
            groups.add(new ArrayList<String>());
            groups.get(index).add(strings[i]);
            considered.add(strings[i]);
            TreeMap<Integer, Integer> list = map.get(strings[i]);
            for (int j = i+1; j < numStrings; j++) {
                TreeMap<Integer, Integer> list2 = map.get(strings[j]);
                if (list.equals(list2) && !considered.contains(strings[j]) && strings[j].length() == strings[i].length()) {
                    groups.get(index).add(strings[j]);
                    considered.add(strings[j]);
                }
            }
            index++;
        }
       
        System.out.println(groups.size());
       
        int max = 0;
        for (int i = 0; i < groups.size(); i++) {
            Collections.sort(groups.get(i), Collections.reverseOrder());
            if (groups.get(i).size() > max) {
                max = groups.get(i).size();
            }
        }
        ArrayList<String> groups2 = new ArrayList<String>();           
        for (int i = 0; i < groups.size(); i++) {
            if (groups.get(i).size() != max) {
                continue;
            }
            StringBuilder sb = new StringBuilder();
            for (int j = 0; j < groups.get(i).size()-1; j++) {
                sb.append(groups.get(i).get(j) + " ");
            }
            sb.append(groups.get(i).get(groups.get(i).size()-1));
            groups2.add(sb.toString());
        }
       
        Collections.sort(groups2);
        for (int i = 0; i < groups2.size(); i++) {
            System.out.println(groups2.get(i));
        }
    }
}

Comments

Popular posts from this blog

(Coderbyte) Dash Insert II - Solução

(Coderbyte) Run Length - Solução

(Coderbyte) Counting Minutes I - Solução