-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMax min
80 lines (62 loc) · 1.79 KB
/
Max min
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
Given two strings, determine if they share a common substring. A substring may be as small as one character.
For example, the words "a", "and", "art" share the common substring . The words "be" and "cat" do not share a substring.
Function Description
Complete the function twoStrings in the editor below. It should return a string, either YES or NO based on whether the strings share a common substring.
twoStrings has the following parameter(s):
s1, s2: two strings to analyze .
Input Format
The first line contains a single integer , the number of test cases.
The following pairs of lines are as follows:
The first line contains string s1.
The second line contains string s2.
Output Format
For each pair of strings, return YES or NO.
Sample Input
2
hello
world
hi
world
Sample Output
YES
NO
*/
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class Solution
{
public static Set<Character> toCharSet(String word)
{
Set<Character> charSet = new HashSet<Character>();
for (int i = 0; i < word.length(); i++)
{
charSet.add(word.charAt(i));
}
return charSet;
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int q = sc.nextInt();
for (int qItr = 0; qItr < q; qItr++)
{
Set<Character> s1 = toCharSet(sc.next());
Set<Character> s2 = toCharSet(sc.next());
s1.retainAll(s2);
if (s1.size() > 0)
{
System.out.println("YES");
}
else
{
System.out.println("NO");
}
}
}
}