Created
March 19, 2016 01:29
-
-
Save jianminchen/acedbb7cb86cf1c00131 to your computer and use it in GitHub Desktop.
Two strings - use hashset - more efficient
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace hacker_rank | |
{ | |
class Solution | |
{ | |
static void Main(string[] args) | |
{ | |
var n = int.Parse(Console.ReadLine()); | |
for (var i = 0; i < n; i++) | |
{ | |
var a = Console.ReadLine(); | |
var b = Console.ReadLine(); | |
var aChars = new HashSet<char>(a.ToList()); | |
var bChars = b.ToList(); | |
if (bChars.Any(c => aChars.Contains(c))) | |
{ | |
Console.WriteLine("YES"); | |
} | |
else | |
{ | |
Console.WriteLine("NO"); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
June 16, 2016
Review HashSet constructor - IEnumerable collection, read example code:
https://msdn.microsoft.com/en-us/library/bb301504(v=vs.110).aspx
Any - HashSet extension method LINQ - set operation - Any