Ransom Note

https://www.lintcode.com/en/problem/ransom-note/description

public class Solution {
    /**
     * @param ransomNote: a string
     * @param magazine: a string
     * @return: whether the ransom note can be constructed from the magazines
     */
    public boolean canConstruct(String ransomNote, String magazine) {
        // Write your code here
        int[] letters = new int[26];
        char[] chars = magazine.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            char aChar = chars[i];
            letters[aChar - 'a']++;
        }
        char[] chars1 = ransomNote.toCharArray();
        for (int i = 0; i < chars1.length; i++) {
            char c = chars1[i];
            letters[c - 'a']--;
            if (letters[c - 'a'] < 0) {
                return false;
            }
        }
        return true;
    }
}

你可能感兴趣的:(Ransom Note)