Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created March 21, 2016 19:31
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/ee9fbf9612c8ada8ccae to your computer and use it in GitHub Desktop.
Save jianminchen/ee9fbf9612c8ada8ccae to your computer and use it in GitHub Desktop.
Two string - JavaScript - using object literal - creating a hashtable, and then, first string goes into as Hashtable - add it if not in; second string, add one on existing one.
function processData(input) {
var inp = input.split("\n"),
i;
for(i=1; i< parseInt(inp[0]) + 1; i++){
console.log(hasCommonChar(inp[i*2-1], inp[i*2])? "YES":"NO");
}
}
function hasCommonChar(a, b){
var i = a.length,
map = {},
c;
while(i--){
if(!map[a[i]]){
map[a[i]] = 1;
}
}
i = b.length;
while(i--){
if(map[b[i]]){
map[b[i]]++;
}
}
for(c in map){
if(map[c] > 1){
return true;
}
}
}
process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
_input += input;
});
process.stdin.on("end", function () {
processData(_input);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment