Created
March 21, 2016 19:00
-
-
Save jianminchen/fe302cf65ac762e7f3eb to your computer and use it in GitHub Desktop.
Two string - JavaScript code - implement the function to add unique char into the array
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) { | |
input = input.split('\n'); | |
var inputIndex = 0; | |
var numCases = Number(input[inputIndex++]); | |
for (var i = 0; i < numCases; i++) { | |
//var wordA = processWord(input[inputIndex++]); | |
//var wordB = processWord(input[inputIndex++]); | |
var wordA = uniq_fast(input[inputIndex++]); | |
var wordB = uniq_fast(input[inputIndex++]); | |
//console.log(wordA); | |
//console.log(wordB); | |
if (searchWords(wordA, wordB)) { | |
console.log("YES"); | |
} else { | |
console.log("NO"); | |
} | |
} | |
} | |
function searchWords(wordA, wordB) { | |
"use strict"; | |
for (var j = 0; j < wordA.length; j++) { | |
for (var k = 0; k < wordB.length; k++) { | |
if (wordA[j] == wordB[k]) { | |
return true; | |
} | |
} | |
} | |
return false; | |
} | |
function uniq_fast(a) { | |
var seen = {}; | |
var out = []; | |
var len = a.length; | |
var j = 0; | |
for(var i = 0; i < len; i++) { | |
var item = a[i]; | |
if(seen[item] !== 1) { | |
seen[item] = 1; | |
out[j++] = item; | |
} | |
} | |
return out; | |
} | |
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