Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created March 19, 2016 01:58
Show Gist options
  • Save jianminchen/7de04dcfb8740ee19412 to your computer and use it in GitHub Desktop.
Save jianminchen/7de04dcfb8740ee19412 to your computer and use it in GitHub Desktop.
Two strings - using two pointers, sort array first; Not bad if not considering time complexity.
using System;
using System.Collections.Generic;
using System.IO;
class Solution {
static void Main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */
int T = int.Parse(Console.ReadLine());
for(int i=0; i<T; i++){
String A = Console.ReadLine();
String B = Console.ReadLine();
Boolean flag = false;
char [] a = A.ToCharArray();
Array.Sort(a);
char [] b = B.ToCharArray();
Array.Sort(b);
int j=0,k=0;
while(j<a.Length && k<b.Length){
if(a[j]==b[k]){
flag = true;
break;
}
else if(a[j]<b[k])
j++;
else
k++;
}
if(flag)
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