Java O(n) 12ms Solution


  • 0
        public boolean canConstruct(String ransomNote, String magazine) {
            int[] ransomNoteCount=countChars(ransomNote);
            int[] magazineCount=countChars(magazine);
            for(int i=0;i<26;i++){
            	if (ransomNoteCount[i]>magazineCount[i]) {
    				return false;
    			}
            }
            return true;
        }
    
    	public int[] countChars(String s) {
    		int[] count = new int[26];
    		int length = s.length();
    		char[] chars=s.toCharArray();
    		for (int i = 0; i < length; i++) {
    			count[chars[i] - 0x61]++;
    		}
    		return count;
    	}

Log in to reply
 

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