Ransom Note

Back to Homepage   |     Back to Code List


class Solution {
    public boolean canConstruct(String ransomNote, String magazine) {
       int[] cs = new int[26];
        for (char c : magazine.toCharArray()) {
            cs[c - 'a']++;
        }
        
        for (char c : ransomNote.toCharArray()) {
            if (--cs[c - 'a'] < 0) return false;
        }
        
        return true;
    }
}