-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathransom_note.dart
88 lines (58 loc) · 1.88 KB
/
ransom_note.dart
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
-* Ransom Note *-
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
Example 3:
Input: ransomNote = "aa", magazine = "aab"
Output: true
Constraints:
1 <= ransomNote.length, magazine.length <= 105
ransomNote and magazine consist of lowercase English letters.
*/
/*
*/
import 'dart:collection';
class A {
bool canConstruct(String ransomNote, String magazine) {
// Base case
if (ransomNote.length > magazine.length) return false;
// .. English Alphabets
List<int> alphabetCounter = List.filled(26, 0);
// every element in the magazine
// i represents as Index
for (String i in magazine.split("")) {
// saving the index value of each and every alphabet based on Character Code
alphabetCounter[i.codeUnitAt(0) - 'a'.codeUnitAt(0)]++;
}
// every element in the ransom Note
for (String i in ransomNote.split("")) {
// if index is zero means we can not construct String because it does not exist
if (alphabetCounter[i.codeUnitAt(0) - 'a'.codeUnitAt(0)] == 0) {
return false;
} else {
// else we decrement that character exist
alphabetCounter[i.codeUnitAt(0) - 'a'.codeUnitAt(0)]--;
}
}
// all other possible cases
return true;
}
}
class B {
bool canConstruct(String ransomNote, String magazine) {
HashMap<String, int> map = HashMap();
for (String c in magazine.split(""))
map[c] = map.putIfAbsent(c, () => 0) + 1;
for (String c in ransomNote.split("")) {
if (!map.containsKey(c) || map[c]! < 1) return false;
map[c] = map[c]! - 1;
}
return true;
}
}