You are viewing a single comment's thread. Return to all comments →
Here is mine:
try (final Scanner in = new Scanner(System.in)) { final int numMagazineWords = in.nextInt(); final int numRansomWords = in.nextInt(); final Map<String, Integer> magazineWordsToCounts = new HashMap<>(); IntStream.range(0, numMagazineWords).mapToObj(i -> in.next()) .forEach(word -> magazineWordsToCounts.merge(word, 1, (a, b) -> a + b)); final Map<String, Integer> ransomWordsToCounts = new HashMap<>(); IntStream.range(0, numRansomWords).mapToObj(i -> in.next()) .forEach(word -> ransomWordsToCounts.merge(word, 1, (a, b) -> a + b)); boolean ransomNotePossible = true; for (final String word : ransomWordsToCounts.keySet()) { if (ransomWordsToCounts.get(word) > magazineWordsToCounts.getOrDefault(word, 0)) { ransomNotePossible = false; break; } } if (ransomNotePossible) { System.out.println("Yes"); } else { System.out.println("No"); } }
Hash Tables: Ransom Note
You are viewing a single comment's thread. Return to all comments →
Here is mine: