Skip to content

Commit b1fed93

Browse files
committed
https://leetcode.com/problems/ransom-note/
1 parent d9e5593 commit b1fed93

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
namespace LeetCodeSolutions.HashMap
2+
{
3+
public class RansomNote
4+
{
5+
public bool CanConstruct(string ransomNote, string magazine)
6+
{
7+
Dictionary<char, int> counts = new Dictionary<char, int>();
8+
9+
foreach (char c in magazine)
10+
{
11+
if (counts.ContainsKey(c))
12+
{
13+
counts[c]++;
14+
}
15+
else
16+
{
17+
counts.Add(c, 1);
18+
}
19+
}
20+
21+
foreach (char c in ransomNote)
22+
{
23+
if (counts.ContainsKey(c) && counts[c] > 0)
24+
{
25+
counts[c]--;
26+
}
27+
else
28+
{
29+
return false;
30+
}
31+
}
32+
33+
return true;
34+
}
35+
36+
[Test(Description = "https://leetcode.com/problems/ransom-note/")]
37+
[Category("Easy")]
38+
[Category("LeetCode")]
39+
[Category("Ransom Node")]
40+
[TestCaseSource(nameof(Input))]
41+
[Category("HashMap")]
42+
[Category("TopInterview")]
43+
public void Test1((bool Output, (string, string) Input) item)
44+
{
45+
var response = CanConstruct(item.Input.Item1, item.Input.Item2);
46+
Assert.That(item.Output, Is.EqualTo(response));
47+
}
48+
49+
public static IEnumerable<(bool Output, (string, string) Input)> Input =>
50+
new List<(bool Output, (string, string) Input)>()
51+
{
52+
(false, ("aa", "ab")),
53+
};
54+
}
55+
}

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
![](https://img.shields.io/badge/Neovim-57A143.svg?logo=Neovim&amp;logoColor=white)
44
![](https://img.shields.io/badge/Visual_Studio_Code-0078D4?logo=visual%20studio%20code&amp;logoColor=white)
5-
![](https://img.shields.io/badge/Progress-25%2F150-0078D4)
5+
![](https://img.shields.io/badge/Progress-26%2F150-0078D4)
66

77
This repository contains solutions to the Leetcode Top Interview 150 problems.
88

@@ -86,7 +86,7 @@ The Top Interview 150 collection on Leetcode is a curated set of 150 interview q
8686
| 37 | Set Matrix Zeroes | Medium | |
8787
| 38 | Game of Life | Medium | |
8888
| <br> Hashmap<br> | | | |
89-
| 39 | Ransom Note | Easy | |
89+
| 39 | Ransom Note | Easy | |
9090
| 40 | Isomorphic Strings | Easy ||
9191
| 41 | Word Pattern | Easy | |
9292
| 42 | Valid Anagram | Easy | |

0 commit comments

Comments
 (0)