-
Notifications
You must be signed in to change notification settings - Fork 3
/
Sparse Arrays.swift
63 lines (47 loc) · 1.76 KB
/
Sparse Arrays.swift
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
import Foundation
/*
* Complete the 'matchingStrings' function below.
*
* The function is expected to return an INTEGER_ARRAY.
* The function accepts following parameters:
* 1. STRING_ARRAY strings
* 2. STRING_ARRAY queries
*/
func matchingStrings(strings: [String], queries: [String]) -> [Int] {
var dictionary: Dictionary<String, Int> = [:]
var results: [Int] = []
queries.forEach {
dictionary.updateValue(0, forKey: $0)
}
strings.forEach {
if let value = dictionary[$0] {
dictionary.updateValue(value + 1, forKey: $0)
}
}
queries.forEach {
results.append(dictionary[$0]!)
}
return results
}
let stdout = ProcessInfo.processInfo.environment["OUTPUT_PATH"]!
FileManager.default.createFile(atPath: stdout, contents: nil, attributes: nil)
let fileHandle = FileHandle(forWritingAtPath: stdout)!
guard let stringsCount = Int((readLine()?.trimmingCharacters(in: .whitespacesAndNewlines))!)
else { fatalError("Bad input") }
var strings = [String]()
for _ in 1...stringsCount {
guard let stringsItem = readLine() else { fatalError("Bad input") }
strings.append(stringsItem)
}
guard strings.count == stringsCount else { fatalError("Bad input") }
guard let queriesCount = Int((readLine()?.trimmingCharacters(in: .whitespacesAndNewlines))!)
else { fatalError("Bad input") }
var queries = [String]()
for _ in 1...queriesCount {
guard let queriesItem = readLine() else { fatalError("Bad input") }
queries.append(queriesItem)
}
guard queries.count == queriesCount else { fatalError("Bad input") }
let res = matchingStrings(strings: strings, queries: queries)
fileHandle.write(res.map{ String($0) }.joined(separator: "\n").data(using: .utf8)!)
fileHandle.write("\n".data(using: .utf8)!)