-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
45 lines (38 loc) · 1.19 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
public class Solution
{
/*
Given two strings ransomNote and magazine, return true if ransomNote can be constructed by using the letters from magazine and false otherwise.
Each letter in magazine can only be used once in ransomNote.
Example 1:
Input: ransomNote = "a", magazine = "b"
Output: false
Example 2:
Input: ransomNote = "aa", magazine = "ab"
Output: false
*/
public static bool CanConstruct(string ransomNote, string magazine)
{
if(ransomNote.Length > magazine.Length)
{
return false;
}
List<char> charList = new List<char>(magazine.ToCharArray());
for (int i = 0; i < ransomNote.Length; i++)
{
if (charList.Contains(ransomNote[i])){
charList.Remove(ransomNote[i]);
}
else
{
return false;
}
}
return true;
}
public static void Main()
{
Console.WriteLine(CanConstruct( "note" , "notebook"));
Console.WriteLine(CanConstruct("a", "b"));
Console.WriteLine(CanConstruct("aa", "ab"));
}
}