# 原题

Given  an  arbitrary  ransom  note  string  and  another  string  containing  letters from  all  the  magazines,  write  a  function  that  will  return  true  if  the  ransom   note  can  be  constructed  from  the  magazines ;  otherwise,  it  will  return  false.

Each  letter  in  the  magazine  string  can  only  be  used  once  in  your  ransom  note.

Note:
You may assume that both strings contain only lowercase letters.

canConstruct("a", "b") -> false
canConstruct("aa", "ab") -> false
canConstruct("aa", "aab") -> true


# 解法

    public boolean canConstruct(String ransomNote, String magazine) {
boolean ret = true;
byte[] bytes = new byte[magazine.length()];
for (int i = 0; i < ransomNote.length(); i++) {
char c = ransomNote.charAt(i);
boolean found = false;
for (int j = 0; j < magazine.length(); j++) {
if (bytes[j] == 0 && magazine.charAt(j) == c) {
bytes[j]++;
found = true;
break;
}
}
if (!found) {
ret = false;
break;
}
}

return ret;
}

    public boolean canConstruct(String ransomNote, String magazine) {
boolean ret = true;
char[] ra = ransomNote.toCharArray();
Arrays.sort(ra);
char[] ma = magazine.toCharArray();
Arrays.sort(ma);

int index = 0;
boolean found = true;
for (int i = 0; i < ra.length && ret; i++) {
char ri = ra[i];
found = false;
for (int j = index; j < ma.length; j++) {
if (ma[j] > ri) {
ret = false;
break;
} else if (ma[j] == ri) {
index++;
found = true;
break;
} else {
index++;
}
}
if (!found) {
ret = false;
break;
}
}

return ret;
}

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