AC O(N) solution in Java using bucket


  • 1
    H
     public boolean canConstruct(String ransomNote, String magazine) {
            int[] table = new int[26];
            for (int i = 0; i < ransomNote.length(); i++) {
                char c = ransomNote.charAt(i);
                table[c-'a']++;
            }
            
            for (int i = 0; i < magazine.length(); i++) {
                char c = magazine.charAt(i);
                table[c-'a']--;
            }
            
            for (int k : table) {
                if (k > 0) return false;
            }
            return true;
        }
    

Log in to reply
 

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