/
TwoStrings.java
59 lines (43 loc) · 1.25 KB
/
TwoStrings.java
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
package hackerrank.algorithms.strings;
import java.util.HashSet;
import java.util.Set;
/**
* Created by nikoo28 on 10/10/20 4:48 PM
*/
public class TwoStrings {
public String twoStrings(String s1, String s2) {
Set<Character> s1Set = new HashSet<>();
Set<Character> s2Set = new HashSet<>();
//creating the set of string1
for (char c : s1.toCharArray()) {
s1Set.add(c);
}
//creating the set of string2
for (char c : s2.toCharArray()) {
s2Set.add(c);
}
// store the set intersection in s1Set
s1Set.retainAll(s2Set);
if (!s1Set.isEmpty())
return "YES";
return "NO";
}
public String twoStringsMethodTwo(String s1, String s2) {
s1 = s1.toLowerCase();
s2 = s2.toLowerCase();
// Create a frequency array of size 26
char[] freq = new char[26];
// Increase the frequency of each character for string s2
for (int i = 0; i < s2.length(); i++) {
freq[s2.charAt(i) - 'a']++;
}
// Check each character in string s1 for frequency.
// If the frequency is greater than 0, it means you found a substring. Simply return YES
for (int i = 0; i < s1.length(); i++) {
if (freq[s1.charAt(i) - 'a'] > 0) {
return "YES";
}
}
return "NO";
}
}