- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashMap.java
More file actions
Latest commit
56 lines (42 loc) · 1.56 KB
/
Copy pathhashMap.java
File metadata and controls
56 lines (42 loc) · 1.56 KB
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
https://www.hackerrank.com/challenges/two-strings/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=dictionaries-hashmaps
importjava.io.*;
importjava.math.*;
importjava.security.*;
importjava.text.*;
importjava.util.*;
importjava.util.concurrent.*;
importjava.util.regex.*;
publicclassSolution {
// Complete the twoStrings function below.
staticStringtwoStrings(Strings1, Strings2) {
String[] split1 = s1.split("");
String[] split2 = s2.split("");
HashMap<String, Integer> firstMap = newHashMap<String, Integer>();
for( inti = 0; i<split1.length; i++ ){
if( firstMap.get( split1[i] ) == null ){
firstMap.put( split1[i], 1 );
}
}
for( inti = 0; i<split2.length; i++ ){
if( firstMap.get( split2[i] ) != null ){
return"YES";
}
}
return"NO";
}
privatestaticfinalScannerscanner = newScanner(System.in);
publicstaticvoidmain(String[] args) throwsIOException {
BufferedWriterbufferedWriter = newBufferedWriter(newFileWriter(System.getenv("OUTPUT_PATH")));
intq = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
for (intqItr = 0; qItr < q; qItr++) {
Strings1 = scanner.nextLine();
Strings2 = scanner.nextLine();
Stringresult = twoStrings(s1, s2);
bufferedWriter.write(result);
bufferedWriter.newLine();
}
bufferedWriter.close();
scanner.close();
}
}