Java 13ms O(m + n) time O(1) space solution


  • 0
    D
    public class Solution {
        public boolean canConstruct(String ransomNote, String magazine) {
            int[] magcount = new int[26];
            Arrays.fill(magcount, 0);
            for (char c : magazine.toCharArray()) {
                magcount[c - 'a'] += 1;
            }
            
            for (char c : ransomNote.toCharArray()) {
                int index = c - 'a';
                if (magcount[index] == 0) return false;
                magcount[index] -= 1;
            }
            return true;
        }
    }
    

Log in to reply
 

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