来源

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/ransom-note

描述

给定一个赎金信 (ransom) 字符串和一个杂志(magazine)字符串,判断第一个字符串 ransom 能不能由第二个字符串 magazines 里面的字符构成。如果可以构成,返回 true ;否则返回 false。

(题目说明:为了不暴露赎金信字迹,要从杂志上搜索各个需要的字母,组成单词来表达意思。杂志字符串中的每个字符只能在赎金信字符串中使用一次。)

注意:
你可以假设两个字符串均只含有小写字母。
canConstruct(“a”, “b”) -> false
canConstruct(“aa”, “ab”) -> false
canConstruct(“aa”, “aab”) -> true

题解

  1. class Solution {
  2. public boolean canConstruct(String ransomNote, String magazine) {
  3. int[] buckets = new int[26];
  4. for (char c : magazine.toCharArray()) {
  5. buckets[c - 'a']++;
  6. }
  7. for (char c : ransomNote.toCharArray()) {
  8. buckets[c - 'a']--;
  9. if (buckets[c - 'a'] < 0) {
  10. return false;
  11. }
  12. }
  13. return true;
  14. }
  15. }