You are viewing a single comment's thread. Return to all comments →
This changes the inital code but is optimized
Map<String, Integer> magazineMap; boolean isValid; public Solution(String magazine, String note) { magazineMap = new HashMap<String, Integer>(); isValid = true; for (String word : magazine.split(" ")) { Integer i = magazineMap.get(word); if (i == null) { magazineMap.put(word, 1); } else { magazineMap.put(word, i + 1); } } for (String word : note.split(" ")) { Integer i = magazineMap.get(word); if (i == null || magazineMap.get(word) == 0) { isValid = false; break; } else { magazineMap.put(word, i - 1); } } } public boolean solve() { return isValid; }
Hash Tables: Ransom Note
You are viewing a single comment's thread. Return to all comments →
This changes the inital code but is optimized