Created
March 21, 2016 19:31
-
-
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.
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
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