Leetcode:383.Ransom Note(文字列ごとのアルファベット数を統計)


Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be constructed from the magazines ; otherwise, it will return false.
Each letter in the magazine string can only be used once in your ransom note.
Note: You may assume that both strings contain only lowercase letters.
canConstruct(“a”, “b”) -> false canConstruct(“aa”, “ab”) -> false canConstruct(“aa”, “aab”) -> true
注意:
これは文字列を求める問題ではありません.
この問題は私が以前やった問題ととても似ています.クリックしてください.
    public static boolean canConstruct(String ransomNote, String magazine) {
        //  : 26      26    int        0
        int[] arr = new int[26];
        //             abcd......z             
        for (int i = 0; i < magazine.length(); i++) {
            int MIndex =magazine.charAt(i) - 'a'; 
            arr[MIndex]++;
        }   
        for (int i = 0; i < ransomNote.length(); i++) {
            if(--arr[ransomNote.charAt(i)-'a'] < 0) {
                return false;
            }
        }
        return true;
    }