clueless coding // TODO: be smarter

LeetCode Ransom Note: Java Solution



TL;DR Code Solution


public boolean canConstruct(String ransomNote, String magazine) {

    HashMap<Character, Integer> hashMap = new HashMap<Character, Integer>();

    for (int i = 0; i < magazine.length(); i++) {
        char cur = magazine.charAt(i);
        hashMap.put(cur, hashMap.getOrDefault(cur, 0) + 1);
    }


    for (int i = 0; i < ransomNote.length(); i++) {

        char cur = ransomNote.charAt(i);

        if (!hashMap.containsKey(cur) || hashMap.get(cur) < 1) {
            return false;
        }
        hashMap.put(cur, hashMap.get(cur) - 1);


    }

    return true;

}