Skip to content

Commit f9b5700

Browse files
committed
https://leetcode.cn/problems/ransom-note
1 parent d51828d commit f9b5700

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ Step 2. Add the dependency
4949

5050
<summary>展开查看</summary>
5151

52+
https://leetcode.cn/problems/ransom-note
53+
5254
https://leetcode.cn/problems/valid-perfect-square
5355

5456
https://leetcode.cn/problems/insufficient-nodes-in-root-to-leaf-paths

ransom-note/index.cxx

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
module;
2+
#include <unordered_map>
3+
#include <string>
4+
export module ransom_note.Solution;
5+
namespace ransom_note
6+
{
7+
using std::string;
8+
using std::unordered_map;
9+
export class Solution
10+
{
11+
12+
public:
13+
bool canConstruct(string ransomNote, string magazine)
14+
{
15+
16+
auto map1 = unordered_map<char, int>{};
17+
18+
for (auto c : magazine)
19+
{
20+
map1[c]++;
21+
}
22+
23+
for (auto c : ransomNote)
24+
{
25+
map1[c]--;
26+
if (map1[c] < 0)
27+
return false;
28+
}
29+
return true;
30+
}
31+
};
32+
33+
}

ransom-note/index.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export default function canConstruct(
2+
ransomNote: string,
3+
magazine: string,
4+
): boolean {
5+
const map1 = new Map<string, number>();
6+
7+
for (const c of magazine) {
8+
map1.set(c, 1 + ((map1.get(c)) ?? 0));
9+
}
10+
11+
for (const c of ransomNote) {
12+
map1.set(c, -1 + ((map1.get(c)) ?? 0));
13+
14+
if (((map1.get(c)) ?? 0) < 0) return false;
15+
}
16+
return true;
17+
}

0 commit comments

Comments
 (0)