First Thoughts: should return false if the letter does not exist in the first place, or letter used exceeds letter stored. Using hashmap can shorten time of retrieval.
My Solution:
class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
HashMap<Character, Integer> map = new HashMap<>();
for (int i=0; i<magazine.length(); i++) {
char c = magazine.charAt(i);
map.putIfAbsent(c, 0);
map.put(c, map.get(c)+1);
}
for (int j=0; j<ransomNote.length(); j++) {
char ch = ransomNote.charAt(j);
if (map.get(ch)==null) return false;
map.put(ch, map.get(ch)-1);
if (map.get(ch)<0) return false;
}
return true;
}
}