Created
November 28, 2020 15:56
-
-
Save Aldhanekaa/9a67679c770f9d445121b0c6e0e509b5 to your computer and use it in GitHub Desktop.
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
'use strict'; | |
const fs = require('fs'); | |
process.stdin.resume(); | |
process.stdin.setEncoding('utf-8'); | |
let inputString = ''; | |
let currentLine = 0; | |
process.stdin.on('data', function(inputStdin) { | |
inputString += inputStdin; | |
}); | |
process.stdin.on('end', function() { | |
inputString = inputString.split('\n'); | |
main(); | |
}); | |
function readLine() { | |
return inputString[currentLine++]; | |
} | |
/* | |
* Complete the 'stringAnagram' function below. | |
* | |
* The function is expected to return an INTEGER_ARRAY. | |
* The function accepts following parameters: | |
* 1. STRING_ARRAY dictionary | |
* 2. STRING_ARRAY query | |
*/ | |
function stringAnagram(dictionary, query) { | |
// Write your code here | |
let result = []; | |
// for loop isi query | |
// setiap element di query for loop dictionarity | |
// setiap elemen kita jadikan array, lalu for loop lagi | |
// item yg dijadikan array tadi dibandingkan denagn setiap elemen yg berada di dictionarity | |
// nark || rank, narrk | |
query.forEach(e => { | |
let number = 0; | |
const string = e.split(""); | |
dictionary.forEach(anagram => { | |
string.forEach(StringChar => { | |
if (anagram.indexOf(StringChar) !== -1) { | |
let arr = anagram.split("") | |
arr = arr.filter((anagramChar, index) => { | |
if (index != anagram.indexOf(StringChar)) { | |
return anagramChar | |
} | |
}).join(''); | |
if (arr == "") { | |
number++; | |
} | |
anagram = arr | |
} | |
}) | |
}); | |
result.push(number) | |
}) | |
console.log(result) | |
return result; | |
} | |
function main() { | |
const ws = fs.createWriteStream(process.env.OUTPUT_PATH); | |
const dictionaryCount = parseInt(readLine().trim(), 10); | |
let dictionary = []; | |
for (let i = 0; i < dictionaryCount; i++) { | |
const dictionaryItem = readLine(); | |
dictionary.push(dictionaryItem); | |
} | |
const queryCount = parseInt(readLine().trim(), 10); | |
let query = []; | |
for (let i = 0; i < queryCount; i++) { | |
const queryItem = readLine(); | |
query.push(queryItem); | |
} | |
const result = stringAnagram(dictionary, query); | |
ws.write(result.join('\n') + '\n'); | |
ws.end(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
b r u h