JAVA solution -- very easy to understand


  • 0
    Z
    public class Solution {
        public boolean canConstruct(String ransomNote, String magazine) {
            int[] arr1 = new int[26];
            int[] arr2 = new int[26];
            int offset = 'a';
            Arrays.fill(arr1,0);
            Arrays.fill(arr2,0);
            for(int i = 0; i < ransomNote.length(); i++){
                arr1[ransomNote.charAt(i) - offset]++;
            }
            for(int i = 0; i < magazine.length(); i++){
                arr2[magazine.charAt(i) - offset]++;
            }
            for(int i = 0; i < 26; i++){
                if(arr1[i] > arr2[i]) return false;
            }
            return true;
        }
    }
    

Log in to reply
 

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