Created
March 19, 2016 01:26
-
-
Save jianminchen/f73e37b764147cd19c7b to your computer and use it in GitHub Desktop.
HackerRank: Two strings - https://www.hackerrank.com/challenges/two-strings/submissions/code/18506948
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; | |
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