payal-kothari
7/25/2017 - 6:55 PM

From https://leetcode.com/problems/ransom-note/#/description

public class Solution {
    public boolean canConstruct(String ransomNote, String magazine) {
        Map<Character, Integer> map = new HashMap<>();
        boolean flag = true;
        
        for(char c : magazine.toCharArray()){
            int v = map.getOrDefault(c, 0) +1;
            map.put(c, v);
        }
        
        for(char c : ransomNote.toCharArray()){
            int v = map.getOrDefault(c, 0) -1;
            if(v < 0){
                flag = false;
                break;
            }else{
                map.put(c, v);
            }
        }  
        return flag;       
    }
    
}