JavaScript中如何遍历字符串动态添加至HashMap?STDIN字符串存入Map方法
Hey there! Let's tackle your two questions step by step—since you're working on that ransom note problem, these Map operations are exactly what you need to nail the solution.
1. Traversing a String and Adding Entries to a Map Dynamically
First off, if you're looking to count occurrences of individual characters in a string (super useful for some versions of this problem), you can loop through each character with a for...of loop and update the Map as you go. Here's a straightforward example:
const sampleString = "hello there"; const charCountMap = new Map(); // Loop through each character in the string for (const char of sampleString) { // If the character already exists in the Map, increment its count // If not, set its count to 1 charCountMap.set(char, (charCountMap.get(char) || 0) + 1); } console.log(charCountMap); // Output: Map(7) { 'h' => 2, 'e' => 3, 'l' => 2, 'o' => 1, ' ' => 1, 't' => 1, 'r' => 1 }
If instead you're working with a string that's space-separated words (like the input for the ransom note problem), just split the string into an array first, then loop through that array (which you already know how to do!). For example:
const wordString = "give me one grand today night"; const wordCountMap = new Map(); const words = wordString.split(/\s+/); // Split on one or more spaces for (const word of words) { wordCountMap.set(word, (wordCountMap.get(word) || 0) + 1); }
2. Storing Space-Separated Strings from STDIN into a Map
In Node.js (which is what you'd use for HackerRank's JS environment), you'll need to read input from STDIN first, then convert that input into an array of words. The readline module is perfect for this—here's a practical example tailored to the ransom note problem:
const readline = require('readline'); const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); // We'll collect two lines: one for the magazine, one for the ransom note let inputLines = []; rl.on('line', (line) => { inputLines.push(line); // Once we have both lines, process them if (inputLines.length === 2) { // Split each line into words, trimming extra spaces first const magazineWords = inputLines[0].trim().split(/\s+/); const ransomWords = inputLines[1].trim().split(/\s+/); // Build the count map for the magazine const magazineMap = new Map(); for (const word of magazineWords) { magazineMap.set(word, (magazineMap.get(word) || 0) + 1); } // Check if we can construct the ransom note let canMakeNote = true; for (const word of ransomWords) { const currentCount = magazineMap.get(word); if (!currentCount || currentCount === 0) { canMakeNote = false; break; } // Decrement the count since we're "using" the word magazineMap.set(word, currentCount - 1); } console.log(canMakeNote ? "Yes" : "No"); rl.close(); } });
A quick note: using split(/\s+/) instead of split(' ') ensures you handle cases where there are multiple spaces between words, which is common in input data.
内容的提问来源于stack exchange,提问作者Lionel Yang

