Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created March 19, 2016 01:26
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/f73e37b764147cd19c7b to your computer and use it in GitHub Desktop.
Save jianminchen/f73e37b764147cd19c7b to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TwoStrings
{
/*
* HackerRank:
* https://www.hackerrank.com/challenges/two-strings
*
*
*/
class Program
{
static void Main(string[] args)
{
string s1 = Console.ReadLine();
int n1 = Convert.ToInt16(s1);
for (int i = 0; i < n1; i++)
{
string sA = Console.ReadLine();
string sB = Console.ReadLine();
if (hasACommonSubstring(sA, sB))
Console.WriteLine("YES");
else
Console.WriteLine("NO");
}
}
/*
* s1.Length > 1, <=10^5
*/
public static bool hasACommonSubstring(string s1, string s2)
{
bool[] A = getLatinArray(s1);
bool[] B = getLatinArray(s2);
for (int i = 0; i < 26; i++)
{
if (A[i] && B[i])
return true;
}
return false;
}
private static bool[] getLatinArray(string s1)
{
bool[] A = new bool[26];
int count = 0;
foreach (char c in s1)
{
int index = c - 'a';
if (A[index] != true)
{
A[index] = true;
count++;
}
if(count == 26)
break;
}
return A;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment