File tree Expand file tree Collapse file tree 2 files changed +57
-2
lines changed
LeetCodeSolutions/HashMap Expand file tree Collapse file tree 2 files changed +57
-2
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 2
2
3
3
![ ] ( https://img.shields.io/badge/Neovim-57A143.svg?logo=Neovim& ; logoColor=white )
4
4
![ ] ( https://img.shields.io/badge/Visual_Studio_Code-0078D4?logo=visual%20studio%20code& ; logoColor=white )
5
- ![ ] ( https://img.shields.io/badge/Progress-25 %2F150-0078D4 )
5
+ ![ ] ( https://img.shields.io/badge/Progress-26 %2F150-0078D4 )
6
6
7
7
This repository contains solutions to the Leetcode Top Interview 150 problems.
8
8
@@ -86,7 +86,7 @@ The Top Interview 150 collection on Leetcode is a curated set of 150 interview q
86
86
| 37 | Set Matrix Zeroes | Medium | |
87
87
| 38 | Game of Life | Medium | |
88
88
| <br > Hashmap<br > | | | |
89
- | 39 | Ransom Note | Easy | |
89
+ | 39 | Ransom Note | Easy | ✅ |
90
90
| 40 | Isomorphic Strings | Easy | ✅ |
91
91
| 41 | Word Pattern | Easy | |
92
92
| 42 | Valid Anagram | Easy | |
You can’t perform that action at this time.
0 commit comments