Java solution using array O(m+n)


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