Java O(n) solution


  • 0
    M

    Easy to understand solution in Java

    public class Solution {
        public boolean canConstruct(String ransomNote, String magazine) {
            int[] count = new int[26];
    
            for (char c : ransomNote.toCharArray()) {
                count[c - 'a']++;
            }
            
            for (char c : magazine.toCharArray()) {
                count[c - 'a']--;
            }
            
            for (int i = 0; i < 26; i++) {
                if (count[i] > 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.