Easy UnderStand Java Solution


  • 0
    public boolean canConstruct(String ransomNote, String magazine) {
            if (ransomNote == null || ransomNote.length() == 0) return true;
            if (magazine == null || magazine.length() == 0) return false;
            
            int[] ran = new int[26];
            int[] mag = new int[26];
            count(ran, ransomNote);
            count(mag, magazine);
            for (int i = 0; i < 26; i++) {
                if (ran[i] == 0) {
                    continue;
                }
                if (ran[i] > mag[i]) {
                    return false;
                }
            }
            return true;
        }
        private void count(int[] abc, String ss) {
            for (char c : ss.toCharArray()) {
                abc[c - 'a']++;
            }
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.