Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created March 21, 2016 23:34
Show Gist options
  • Save jianminchen/bfe92a2556aab268d250 to your computer and use it in GitHub Desktop.
Save jianminchen/bfe92a2556aab268d250 to your computer and use it in GitHub Desktop.
Two string - JavaScript - Array.ForEach, a function getNumberArray - code sample
/// <reference path="typings/node/node.d.ts"/>
'use strict';
var _input = "";
function getNumberArray(line) {
var arr = [];
var addNumber = function (val) {
arr.push(parseInt(val));
};
line.split(' ').forEach(addNumber);
return arr;
}
function hasSubString(word1, word2) {
var map = {};
for (var char= 0; char< word1.length; char++)
map[word1.charCodeAt(char)]= 1;
for (var char= 0; char< word2.length; char++) {
if (map.hasOwnProperty(word2.charCodeAt(char)))
return true;
}
return false;
}
function processData(input) {
var lines = input.split('\n').splice(0);
var line = 0;
var tests = getNumberArray(lines[line++])[0];
for (var test = 0; test< tests; test++) {
var word1= lines[line++];
var word2= lines[line++];
if (hasSubString(word1, word2))
console.log("YES");
else
console.log("NO");
}
}
// get the input data
if (_input.length == 0) {
process.stdin.resume();
process.stdin.setEncoding("ascii");
process.stdin.on("data", function (input) {
_input += input;
});
process.stdin.on("end", function () {
processData(_input);
});
}
else
processData(_input);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment