18ms JAVA Solution

  • 1
    public class Solution {
        public boolean canConstruct(String ransomNote, String magazine) {
            int[] map = new int[26];
            for(int i=0; i<magazine.length(); i++)
                map[magazine.charAt(i) - 'a']++;
            for(int i=0; i<ransomNote.length(); i++)
                map[ransomNote.charAt(i) - 'a']--;
            for(int num: map)
                if(num < 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.