-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTwo_Strings.java
More file actions
44 lines (35 loc) · 1.09 KB
/
Two_Strings.java
File metadata and controls
44 lines (35 loc) · 1.09 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
package hackerRank;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
/**
* You are given two strings, AA and BB. Find if there is a substring that appears in both AA and BB.
* @author Debosmit
*
*/
public class Two_Strings {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int numTestCases = scan.nextInt();
if(numTestCases < 1 || numTestCases > 10)
throw new IllegalArgumentException();
while(numTestCases > 0) {
String a = scan.next();
String b = scan.next();
System.out.println(substringPossible(a, b) ? "YES" : "NO");
numTestCases--;
}
}
private static boolean substringPossible(String a, String b) {
Set<Character> setA = new HashSet<Character>();
for(int i = 0 ; i < a.length() ; i++)
setA.add(a.charAt(i));
Set<Character> setB = new HashSet<Character>();
for(int i = 0 ; i < b.length() ; i++)
setB.add(b.charAt(i));
for(Character c: setA)
if(!setB.add(c))
return true;
return false;
}
}