Java. O(n). Self Explanatory.


  • 0
    B

    Key Concept: Frequency table

            int[] a2 = new int[26];
            
            for(int i=0;i<ransomNote.length();i++)
                a1[(int)ransomNote.charAt(i)-97]++;
                
            for(int i=0;i<magazine.length();i++)
                a2[(int)magazine.charAt(i)-97]++;
                
            for(int i=0;i<26;i++) {
                if(a1[i]>a2[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.