Last active
May 22, 2020 19:14
-
-
Save rakibulalam/3cd862b72fac799e99be06c911fd4694 to your computer and use it in GitHub Desktop.
Sparse Arrays Hacker Rank
This file contains hidden or 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
'use strict'; | |
const fs = require('fs'); | |
process.stdin.resume(); | |
process.stdin.setEncoding('utf-8'); | |
let inputString = ''; | |
let currentLine = 0; | |
process.stdin.on('data', inputStdin => { | |
inputString += inputStdin; | |
}); | |
process.stdin.on('end', _ => { | |
inputString = inputString.replace(/\s*$/, '') | |
.split('\n') | |
.map(str => str.replace(/\s*$/, '')); | |
main(); | |
}); | |
function readLine() { | |
return inputString[currentLine++]; | |
} | |
// Complete the matchingStrings function below. | |
function matchingStrings(strings, queries) { | |
const hasTable = strings.reduce((memo, value) => (memo[value] ? memo[value]++ : memo[value] = 1, memo) | |
, {}) | |
return queries.map((value) => hasTable[value] || 0) | |
} | |
function main() { | |
const ws = fs.createWriteStream(process.env.OUTPUT_PATH); | |
const stringsCount = parseInt(readLine(), 10); | |
let strings = []; | |
for (let i = 0; i < stringsCount; i++) { | |
const stringsItem = readLine(); | |
strings.push(stringsItem); | |
} | |
const queriesCount = parseInt(readLine(), 10); | |
let queries = []; | |
for (let i = 0; i < queriesCount; i++) { | |
const queriesItem = readLine(); | |
queries.push(queriesItem); | |
} | |
let res = matchingStrings(strings, queries); | |
ws.write(res.join("\n") + "\n"); | |
ws.end(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment