Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created March 19, 2016 01:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/acedbb7cb86cf1c00131 to your computer and use it in GitHub Desktop.
Save jianminchen/acedbb7cb86cf1c00131 to your computer and use it in GitHub Desktop.
Two strings - use hashset - more efficient
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");
}
}
}
}
}
@jianminchen
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment