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

### 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
• t means Toshko the padawan
• s means Slav the padawan
• y 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
 Input Output 2 m1 k1 p1 t1 s1 m2 p2 t1 s1 m1 m2 k1 p1 p2

 Input Output 1 p4 p2 p3 m1 k2 p1 k1 s1 t1 y1 m1 k2 k1 s1 t1 p4 p2 p3 p1

+1 vote

Here's the solution:

```import java.io.BufferedReader;
import java.io.IOException;

public class Pr_01_JediMeditation {
public static void main(String[] args) throws IOException {

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

boolean isYodaExist = false;
for (String allJedi : allJedis) {
String currentJediType = allJedi.substring(0, 1);

switch (currentJediType) {
case "m":
break;
case "k":
break;
case "p":
break;
case "t":
case "s":
break;
case "y":
isYodaExist = true;
break;
}
}
if (isYodaExist) {
outputOrder[0] = jediMasters;
outputOrder[1] = jediKnights;
outputOrder[2] = jediSmartAss;
} else {
outputOrder[0] = jediSmartAss;
outputOrder[1] = jediMasters;
outputOrder[2] = jediKnights;
}

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());
}
}```