-
Notifications
You must be signed in to change notification settings - Fork 0
/
31-TwoStrings-HashSet.cs
55 lines (47 loc) · 1.37 KB
/
31-TwoStrings-HashSet.cs
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
using System;
using System.Collections.Generic;
using System.Text;
namespace HackerRank
{
class TwoStrings_HashSet_Solution
{
public static void twoStrings(string s1, string s2)
{
//hashset always store only distinct values, no duplication.
HashSet<char> string1_char = new HashSet<char>();
HashSet<char> string2_char = new HashSet<char>();
//Fill distinct charters of s1
foreach(char ch in s1)
{
string1_char.Add(ch);
}
//Fill distinct charters of s2
foreach (char ch in s2)
{
string2_char.Add(ch);
}
//intersects keep common values.
string1_char.IntersectWith(string2_char);
if(string1_char.Count > 0)
{
Console.WriteLine("YES");
return;
}
Console.WriteLine("NO");
return;
}
}
class TwoStrings_HashSet_Result
{
public static void Input()
{
//string s1 = "and";
//string s2 = "art";
string s1 = "be";
string s2 = "cat";
//string s1 = "hello";
//string s2 = "world";
TwoStrings_HashSet_Solution.twoStrings(s1, s2);
}
}
}