-
Notifications
You must be signed in to change notification settings - Fork 0
/
Nuts and bolts problem
62 lines (48 loc) · 1.63 KB
/
Nuts and bolts problem
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
61
62
//Nuts and bolts problem
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int tc = Integer.parseInt(br.readLine().trim());
while(tc-- > 0) {
String[] inputLine;
int n = Integer.parseInt(br.readLine().trim());
char[] nuts = new char[n], bolts = new char[n];
inputLine = br.readLine().trim().split(" ");
for(int i = 0; i < n; i++) {
nuts[i] = (inputLine[i].charAt(0));
}
inputLine = br.readLine().trim().split(" ");
for(int i = 0; i < n; i++) {
bolts[i] = (inputLine[i].charAt(0));
}
new Solution().matchPairs(n, nuts, bolts);
for(int i = 0; i < n; i++) {
System.out.print(nuts[i] + " ");
}
System.out.println();
for(int i = 0; i < n; i++) {
System.out.print(bolts[i] + " ");
}
System.out.println();
}
}
}
class Solution {
void matchPairs(int n, char nuts[], char bolts[]) {
char arr[] = { '!', '#', '$' , '%', '&', '*', '?', '@' , '^'};
HashSet<Character> set = new HashSet<>();
for(char ch : nuts) {
set.add(ch);
}
int k = 0;
for(int i = 0; i < arr.length; i++) {
if(set.contains(arr[i])) {
nuts[k] = arr[i];
bolts[k] = arr[i];
k++;
}
}
}
}