HW1/src/com/company/Main.java
//Each letter in magazine can only be used once in ransomNote.
Each letter in magazine can only be used once in ransomNote.
Each letter in magazine can only be used once in ransomNote .
public static boolean ransomNote(String ransomNote, String magazine) {
        Map<Character, Integer> magazineMap = new HashMap<>();
        Map<Character, Integer> ransomNoteMap = new HashMap<>();
        for (char c : magazine.toCharArray()) {
            magazineMap.put(c, magazineMap.getOrDefault(c, 0) + 1);
        }
        for (char c : ransomNote.toCharArray()) {
            if (!magazineMap.containsKey(c)) return false;
            else if (magazineMap.get(c) == 0) {
                magazineMap.remove(c);
            } else {
                magazineMap.put(c, magazineMap.get(c) - 1);
            }
        }
        return true;
    }
public static boolean ransomNote ( String ransomNote , String magazine ) { Map < Character , Integer > magazineMap = new HashMap < > ( ) ; Map < Character , Integer > ransomNoteMap = new HashMap < > ( ) ; for ( char c : magazine . toCharArray ( ) ) { magazineMap . put ( c , magazineMap . getOrDefault ( c , 0 ) + 1 ) ; } for ( char c : ransomNote . toCharArray ( ) ) { if ( ! magazineMap . containsKey ( c ) ) return false ; else if ( magazineMap . get ( c ) == 0 ) { magazineMap . remove ( c ) ; } else { magazineMap . put ( c , magazineMap . get ( c ) - 1 ) ; } } return true ; }
Each letter in magazine can only be used once in ransomNote.
Each letter in magazine can only be used once in ransomNote .