There is a collection of input strings and a collection of query strings. For each query string, determine how many times it occurs in the list of input strings. Return an array of the results.
這裡有收集一個輸入的字串和一個查詢的字串。對於每個查詢字串皆須確定輸入的字串列表中出現了幾次。 回傳一個包含此結果的陣列。
Example 1:
strings = ['ab', 'ab', 'abc'] queries = ['ab', 'abc', 'bc'] There are 2 instances of 'ab', 1 of 'abc' and 0 of 'bc'. For each query, add an element to the return array, results = [2, 1, 0].
solution:
先建立 hashTable 物件來紀錄 string 陣列中的字串與次數,
在運用 for 迴圈來將符合 queries 中字串的次數更新到 result 陣列中。
Code 1: BigO(2n)
'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 'matchingStrings' function below.
*
* The function is expected to return an INTEGER_ARRAY.
* The function accepts following parameters:
* 1. STRING_ARRAY strings
* 2. STRING_ARRAY queries
*/
function matchingStrings(strings, queries) {
// Write your code here
let hashTable = {}, result = []
for (let i = 0; i < strings.length; i++) {
hashTable[strings[i]] = (hashTable[strings[i]] || 0) + 1
}
for (let j = 0; j < queries.length; j++) {
if (hashTable[queries[j]]) {
result[j] = hashTable[queries[j]]
} else {
result[j] = 0
}
}
return result;
}
function main() {
const ws = fs.createWriteStream(process.env.OUTPUT_PATH);
const stringsCount = parseInt(readLine().trim(), 10);
let strings = [];
for (let i = 0; i < stringsCount; i++) {
const stringsItem = readLine();
strings.push(stringsItem);
}
const queriesCount = parseInt(readLine().trim(), 10);
let queries = [];
for (let i = 0; i < queriesCount; i++) {
const queriesItem = readLine();
queries.push(queriesItem);
}
const res = matchingStrings(strings, queries);
ws.write(res.join('\n') + '\n');
ws.end();
}
FlowChart:
Example 1
console.log(matchingStrings(['ab', 'ab', 'abc'], ['ab', 'abc', 'bc']));
hashTable = { ab: 2, abc: 1 }
result = []
result = [ 2 ]
result = [ 2, 1 ]
result = [ 2, 1, 0 ]