> For the complete documentation index, see [llms.txt](https://leetcodebook-1.gitbook.io/top-interview-150/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://leetcodebook-1.gitbook.io/top-interview-150/hash-table/ransom-note.md).

# 赎金信

题目链接: <https://leetcode.cn/problems/ransom-note/>

## 解题思路：

1. 统计magazine里面每个字符出现的次数,统一用map进行存储
2. 遍历ransomNote,对于ransomNote中出现的每个字符，从步骤1的map中扣减相应的次数，如果map中某个字符的数量扣减至负数，则说明个数不足，magazine无法构成ransomNote

## 答案

```go
func canConstruct(ransomNote, magazine string) bool {
    if len(ransomNote) > len(magazine) {
        return false
    }
    magazineMap := [26]int{}
    for _, item := range magazine {
        magazineMap[item-'a']++
    }
    for _, item := range ransomNote {
        magazineMap[item-'a']--
        if magazineMap[item-'a'] < 0 {
            return false
        }
    }
    return true
}
```

## 复杂度分析

* 时间复杂度：$$O(n)$$，其中 $$n$$ 是数组中的元素数量
* 空间复杂度：$$O(n)$$，其中 $$n$$ 是数组中的元素数量，`map` 开销


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://leetcodebook-1.gitbook.io/top-interview-150/hash-table/ransom-note.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
