[Solved] Jedi Meditation - Java Task

  

8
Topic starter

A long time ago, in a galaxy far, far away...

All Jedi must meditate. Yet, when the Jedi are at their temple, they cannot mediate at the same time, because the temple will overload itself with too much force and an implosion will occur. There is a strict order for meditations: Jedi Masters mediate firstthen Jedi Knights, and lastly – the Padawans.

Given the sequence of Jedi:

{Jedi Type}{Jedi Level}

p1 k1 m2 m1 k2 p2

They will meditate in the following order:

m2 m1 k1 k2 p1 p2

m means a Jedi Master, k is a Jedi Knight, and p is a Padawan.

Toshko and Slav are padawans. They want to have as much time with the Force as they can. So they always try to mediate before Jedi Masters, until Jedi Master Yoda shows up and moves them after Jedi Knights and before Jedi Padawans. Given that  they do not want to wait meaninglessly for meditation, you need to help them solve in which order all Jedi will mediate. There can be multiple yodas, but the number identifiers (such as mare unique).

Input:

  • On the first line, you will find the number N – the count of the input lines.
  • On the next N lines you will receive sequences with Jedis, separated by a single space, waiting for meditation
    • m means Jedi master
    • k means Jedi knight
    • p means Jedi padawan
    • means Toshko the padawan
    • means Slav the padawan
    • means Master Yoda

Output:

  • The output consists of a single line.
  • You must print the sequence of jedis, ready for meditation in the correct order, and in the following format:
    • Print each jedis type and level
    • Different jedis are separated by a single space
    • Master Yoda must NOT be printed.

Constraints:

  • 0 < N < 100 000
  • All inputs will be lowercase characters
jedi
1 Answer
8

Here's the solution:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
 
public class Pr_01_JediMeditation {
    public static void main(String[] args) throws IOException {
        BufferedReader sc = new BufferedReader(new InputStreamReader(System.in));
 
        int count = Integer.parseInt(sc.readLine());
 
        StringBuilder inputJudis = new StringBuilder();//USING StringBuilder TO ADD ALL THE Jedis
 
        for (int i = 0; i < count; i++) {
            inputJudis.append(sc.readLine());
            inputJudis.append(" ");
        }
 
        String[] allJedis = inputJudis.toString().split(" ");//PUTTING ALL THE Jedis INTO AN ARRAY OF Strings
 
        LinkedList<String> jediMasters = new LinkedList<>();
        LinkedList<String> jediKnights = new LinkedList<>();
        LinkedList<String> jediPadawans = new LinkedList<>();
        LinkedList<String> jediSmartAss = new LinkedList<>();
 
        boolean isYodaExist = false;
        for (String allJedi : allJedis) {
            String currentJediType = allJedi.substring(0, 1);
 
            switch (currentJediType) {
                case "m":
                    jediMasters.add(allJedi);
                    break;
                case "k":
                    jediKnights.add(allJedi);
                    break;
                case "p":
                    jediPadawans.add(allJedi);
                    break;
                case "t":
                case "s":
                    jediSmartAss.add(allJedi);
                    break;
                case "y":
                    isYodaExist = true;
                    break;
            }
        }
        LinkedList[] outputOrder = new LinkedList[4];
        if (isYodaExist) {
            outputOrder[0] = jediMasters;
            outputOrder[1] = jediKnights;
            outputOrder[2] = jediSmartAss;
            outputOrder[3] = jediPadawans;
        } else {
            outputOrder[0] = jediSmartAss;
            outputOrder[1] = jediMasters;
            outputOrder[2] = jediKnights;
            outputOrder[3] = jediPadawans;
        }
 
        StringBuilder out = new StringBuilder();
        for (LinkedList jedis : outputOrder) {
            int jediCount = jedis.size();
            for (int i = 0; i < jediCount; i++) {
                out.append(String.format("%s ", jedis.poll()));
            }
        }
        System.out.println(out.toString().trim());
    }
}
Share: