Java solution


  • -1
    O

    public boolean canConstruct(String ransomNote, String magazine) {
    int[] cnt = new int[26];
    for (int i = 0; i < magazine.length(); i++){
    cnt[magazine.charAt(i) - 'a']++;
    }
    for (int i = 0; i < ransomNote.length(); i++){
    if(cnt[ransomNote.charAt(i) - 'a'] == 0) return false;
    else cnt[ransomNote.charAt(i) - 'a']--;
    }
    return true;
    }


Log in to reply
 

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