<div style="display: none;" hidden="true" aria-hidden="true" data-nosnippet>Are you an LLM? You can read better optimized documentation at /pages/8777d6.md for this page in Markdown format</div>
宝石与石头
| Category | Difficulty | Likes | Dislikes |
|---|---|---|---|
| algorithms | Easy (85.22%) | 721 | - |
<details style="color: rgb(225, 228, 232); font-family: -apple-system, "system-ui", "Segoe WPC", "Segoe UI", system-ui, Ubuntu, "Droid Sans", sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><summary><strong>Tags</strong></summary><p style="margin-top: 0px; margin-bottom: 0.7em;"><a href="https://leetcode.com/tag/tree" title="https://leetcode.com/tag/tree" style="color: var(--vscode-textLink-foreground); text-decoration: none;"><code style="color: var(--vscode-textLink-foreground); font-family: var(--vscode-editor-font-family, "SF Mono", Monaco, Menlo, Consolas, "Ubuntu Mono", "Liberation Mono", "DejaVu Sans Mono", "Courier New", monospace); font-size: 1em; line-height: 1.357em; white-space: pre-wrap;"></code></a></p></details>
<details style="color: rgb(225, 228, 232); font-family: -apple-system, "system-ui", "Segoe WPC", "Segoe UI", system-ui, Ubuntu, "Droid Sans", sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><summary><strong>Companies</strong></summary><p style="margin-top: 0px; margin-bottom: 0.7em;"><code style="color: var(--vscode-textPreformat-foreground); font-family: var(--vscode-editor-font-family, "SF Mono", Monaco, Menlo, Consolas, "Ubuntu Mono", "Liberation Mono", "DejaVu Sans Mono", "Courier New", monospace); font-size: 1em; line-height: 1.357em; white-space: pre-wrap;"></code></p></details>
给你一个字符串 jewels 代表石头中宝石的类型,另有一个字符串 stones 代表你拥有的石头。 stones 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石。
字母区分大小写,因此 "a" 和 "A" 是不同类型的石头。
示例 1:
输入:jewels = "aA", stones = "aAAbbbb"
输出:3示例 2:
输入:jewels = "z", stones = "ZZ"
输出:0提示:
1 <= jewels.length, stones.length <= 50jewels和stones仅由英文字母组成jewels中的所有字符都是 唯一的
/*
* @Author: 仲灏<izhaong@outlook.com>🌶🌶🌶
* @Date: 2022-11-09 11:42:49
* @LastEditTime: 2022-11-09 11:45:45
* @LastEditors: 仲灏<izhaong@outlook.com>🌶🌶🌶
* @Description:
* @FilePath: /leetcode/771.宝石与石头.ts
*/
/*
* @lc app=leetcode.cn id=771 lang=typescript
*
* [771] 宝石与石头
*
* https://leetcode.cn/problems/jewels-and-stones/description/
*
* algorithms
* Easy (85.22%)
* Likes: 721
* Dislikes: 0
* Total Accepted: 174.5K
* Total Submissions: 204.8K
* Testcase Example: '"aA"\n"aAAbbbb"'
*
* 给你一个字符串 jewels 代表石头中宝石的类型,另有一个字符串 stones 代表你拥有的石头。 stones
* 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石。
*
* 字母区分大小写,因此 "a" 和 "A" 是不同类型的石头。
*
*
*
* 示例 1:
*
*
* 输入:jewels = "aA", stones = "aAAbbbb"
* 输出:3
*
*
* 示例 2:
*
*
* 输入:jewels = "z", stones = "ZZ"
* 输出:0
*
*
*
*
* 提示:
*
*
* 1 <= jewels.length, stones.length <= 50
* jewels 和 stones 仅由英文字母组成
* jewels 中的所有字符都是 唯一的
*
*
*/
// @lc code=start
function numJewelsInStones(jewels: string, stones: string): number {
let num = 0
for (let i = 0; i < stones.length; i++) {
const s = stones[i];
if(jewels.indexOf(s) !== -1) {
num ++
}
}
return num
};
function numJewelsInStones1(jewels: string, stones: string): number {
const jewelsSet = new Set(jewels.split(""));
return stones.split("").reduce((prev, val) => {
return prev + jewelsSet.has(val);
}, 0);
}
// @lc code=end