-
Notifications
You must be signed in to change notification settings - Fork 0
/
StringPuzzle.java
60 lines (48 loc) · 1.44 KB
/
StringPuzzle.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
Name: Yet Another String Puzzle - Basic
Source: Coding Ninjas
Link: https://classroom.codingninjas.com/app/classroom/me/6142/content/77614/offering/839730/problem/5228
*/
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class StringPuzzle {
public static void zig_zag(List<String> words, char[] queries)
{
System.out.println();
Collections.sort(words);
//Your Code goes here
String checked;
for(int j=0; j<queries.length; j++)
{
for(int i=0; i<words.size(); i++)
{
if(words.get(i).charAt(0)==queries[j])
{
System.out.println(words.get(i));
checked = words.remove(i);
i--;
words.add(checked);
break;
}
}
}
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n = s.nextInt();
List<String> list = new ArrayList<>();
for(int i = 0; i < n; i++) {
list.add(s.next());
}
s.nextLine();
String str = s.nextLine();
String[] arr = str.split(" ");
char[] queries = new char[arr.length];
for(int i = 0; i < arr.length; i++) {
queries[i] = arr[i].charAt(0);
}
zig_zag(list, queries);
}
}