-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path383_Ransom_Note.swift
More file actions
58 lines (41 loc) · 1.31 KB
/
383_Ransom_Note.swift
File metadata and controls
58 lines (41 loc) · 1.31 KB
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
/*
Done 07.09.2025. Revisited: N/A
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 <= 10^5
ransomNote and magazine consist of lowercase English letters.
https://www.youtube.com/watch?v=pRpRsrlvmEs
Google
*/
import Foundation
class P383 {
// MARK: - Option 1 (my). Time: O(?). Memory: O(?)
func canConstruct(_ ransomNote: String, _ magazine: String) -> Bool {
var letters: [Character: Int] = [:]
for ch in magazine {
letters[ch] = (letters[ch] ?? 0) + 1
}
for ch in ransomNote {
if let letter = letters[ch], letter > 0 {
letters[ch] = letters[ch]! - 1
} else {
return false
}
}
return true
}
// MARK: - Option 2 (neetcode). Time: O(?). Memory: O(?)
func canConstruct2(_ ransomNote: String, _ magazine: String) -> Bool {
return false
}
}