O(n+m) solution Java


  • 0
    J
        public boolean canConstruct(String ransomNote, String magazine) {
            int[] alp1 = new int[256];
            int[] alp2 = new int[256];
            
            for(char c: ransomNote.toCharArray()){
                alp1[c]++;
            }
            
            for(char c: magazine.toCharArray()){
                alp2[c]++;
            }
            
            for(int i = 0; i < 256; i++){
                if(alp2[i] < alp1[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.