-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHackerrank_TwoStrings.java
More file actions
107 lines (74 loc) · 2.38 KB
/
Hackerrank_TwoStrings.java
File metadata and controls
107 lines (74 loc) · 2.38 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author KD
*
* Two Strings
*
* 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 .
The second line contains string .
Constraints
and consist of characters in the range ascii[a-z].
Output Format
For each pair of strings, return YES or NO.
Sample Input
2
hello
world
hi
world
Sample Output
YES
NO
Explanation
We have pairs to check:
, . The substrings and are common to both strings.
, . and share no common substrings.
*/
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 Hackerrank_TwoStrings {
}
class Solutionkid {
// Complete the twoStrings function below.
static String twoStrings(String s1, String s2) {
char[] arr = new char[26];
for (char c: s1.toCharArray()) arr[c-'a']++;
for (char c: s2.toCharArray())
if (arr[c-'a'] > 0) return "YES";
return "NO";
}
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws IOException {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
int q = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
for (int qItr = 0; qItr < q; qItr++) {
String s1 = scanner.nextLine();
String s2 = scanner.nextLine();
String result = twoStrings(s1, s2);
bufferedWriter.write(result);
bufferedWriter.newLine();
}
bufferedWriter.close();
scanner.close();
}
}