返回介绍

solution / 0300-0399 / 0383.Ransom Note / README_EN

发布于 2024-06-17 01:04:01 字数 5059 浏览 0 评论 0 收藏 0

383. Ransom Note

中文文档

Description

Given two strings ransomNote and magazine, return true_ if _ransomNote_ can be constructed by using the letters from _magazine_ and _false_ otherwise_.

Each letter in magazine can only be used once in ransomNote.

 

Example 1:

Input: ransomNote = "a", magazine = "b"
Output: false

Example 2:

Input: ransomNote = "aa", magazine = "ab"
Output: false

Example 3:

Input: ransomNote = "aa", magazine = "aab"
Output: true

 

Constraints:

  • 1 <= ransomNote.length, magazine.length <= 105
  • ransomNote and magazine consist of lowercase English letters.

Solutions

Solution 1: Hash Table or Array

We can use a hash table or an array $cnt$ of length $26$ to record the number of times each character appears in the string magazine. Then traverse the string ransomNote, for each character $c$ in it, we decrease the number of $c$ by $1$ in $cnt$. If the number of $c$ is less than $0$ after the decrease, it means that the number of $c$ in magazine is not enough, so it cannot be composed of ransomNote, just return $false$.

Otherwise, after the traversal, it means that each character in ransomNote can be found in magazine. Therefore, return $true$.

The time complexity is $O(m + n)$, and the space complexity is $O(C)$. Where $m$ and $n$ are the lengths of the strings ransomNote and magazine respectively; and $C$ is the size of the character set, which is $26$ in this question.

class Solution:
  def canConstruct(self, ransomNote: str, magazine: str) -> bool:
    cnt = Counter(magazine)
    for c in ransomNote:
      cnt[c] -= 1
      if cnt[c] < 0:
        return False
    return True
class Solution {
  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;
      }
    }
    return true;
  }
}
class Solution {
public:
  bool canConstruct(string ransomNote, string magazine) {
    int cnt[26]{};
    for (char& c : magazine) {
      ++cnt[c - 'a'];
    }
    for (char& c : ransomNote) {
      if (--cnt[c - 'a'] < 0) {
        return false;
      }
    }
    return true;
  }
};
func canConstruct(ransomNote string, magazine string) bool {
  cnt := [26]int{}
  for _, c := range magazine {
    cnt[c-'a']++
  }
  for _, c := range ransomNote {
    cnt[c-'a']--
    if cnt[c-'a'] < 0 {
      return false
    }
  }
  return true
}
function canConstruct(ransomNote: string, magazine: string): boolean {
  const cnt: number[] = Array(26).fill(0);
  for (const c of magazine) {
    ++cnt[c.charCodeAt(0) - 97];
  }
  for (const c of ransomNote) {
    if (--cnt[c.charCodeAt(0) - 97] < 0) {
      return false;
    }
  }
  return true;
}
public class Solution {
  public bool CanConstruct(string ransomNote, string magazine) {
    int[] cnt = new int[26];
    foreach (var c in magazine) {
      ++cnt[c - 'a'];
    }
    foreach (var c in ransomNote) {
      if (--cnt[c - 'a'] < 0) {
        return false;
      }
    }
    return true;
  }
}
class Solution {
  /**
   * @param String $ransomNote
   * @param String $magazine
   * @return Boolean
   */
  function canConstruct($ransomNote, $magazine) {
    $arrM = str_split($magazine);
    for ($i = 0; $i < strlen($magazine); $i++) {
      $hashtable[$arrM[$i]] += 1;
    }
    for ($j = 0; $j < strlen($ransomNote); $j++) {
      if (!isset($hashtable[$ransomNote[$j]]) || $hashtable[$ransomNote[$j]] == 0) {
        return false;
      } else {
        $hashtable[$ransomNote[$j]] -= 1;
      }
    }
    return true;
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文