By using our mini forum, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy

Q: Jedi Meditation - Java Task

+11 votes

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 first, then 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 m2 are unique).


  • 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
    • t means Toshko the padawan
    • s means Slav the padawan
    • y means Master Yoda


  • 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.


  • 0 < N < 100 000
  • All inputs will be lowercase characters




m1 k1 p1 t1 s1

m2 p2

t1 s1 m1 m2 k1 p1 p2





p4 p2 p3 m1 k2 p1 k1 s1 t1 y1

m1 k2 k1 s1 t1 p4 p2 p3 p1



asked in Java category by user nikole

1 Answer

+1 vote
Best answer

Here's the solution:

import java.util.LinkedList;

public class Pr_01_JediMeditation {
    public static void main(String[] args) throws IOException {
        BufferedReader sc = new BufferedReader(new InputStreamReader(;

        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(" ");

        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":
                case "k":
                case "p":
                case "t":
                case "s":
                case "y":
                    isYodaExist = true;
        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()));


answered by user sam
selected by user golearnweb