My easy java code


  • 0
    W
    public class Solution {
        public boolean canConstruct(String ransomNote, String magazine) {
            int [] array = new int[26];
            for (int i = 0; i < 26; i++) {
                array[i] = 0;
            }
            for (char c : ransomNote.toCharArray()) {
                array[c-'a']--;
            }
            for (char c : magazine.toCharArray()) {
                array[c-'a']++;
            }
            for (int i = 0; i < 26; i++) {
                if (array[i] < 0) {
                    return false;
                }
            }
            return true;
        }
    }
    

  • 0
    H

    initializing array to 0 is not necessary.


Log in to reply
 

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