diff --git a/lcci/16.02.Words Frequency/README.md b/lcci/16.02.Words Frequency/README.md index b27886435d3b9..9c65dc344a14c 100644 --- a/lcci/16.02.Words Frequency/README.md +++ b/lcci/16.02.Words Frequency/README.md @@ -203,6 +203,22 @@ WordsFrequency.prototype.get = function (word) { */ ``` +```swift +class WordsFrequency { + private var cnt: [String: Int] = [:] + + init(_ book: [String]) { + for word in book { + cnt[word, default: 0] += 1 + } + } + + func get(_ word: String) -> Int { + return cnt[word, default: 0] + } +} +``` + diff --git a/lcci/16.02.Words Frequency/README_EN.md b/lcci/16.02.Words Frequency/README_EN.md index efae82c6c72ef..13b14374647af 100644 --- a/lcci/16.02.Words Frequency/README_EN.md +++ b/lcci/16.02.Words Frequency/README_EN.md @@ -215,6 +215,22 @@ WordsFrequency.prototype.get = function (word) { */ ``` +```swift +class WordsFrequency { + private var cnt: [String: Int] = [:] + + init(_ book: [String]) { + for word in book { + cnt[word, default: 0] += 1 + } + } + + func get(_ word: String) -> Int { + return cnt[word, default: 0] + } +} +``` + diff --git a/lcci/16.02.Words Frequency/Solution.swift b/lcci/16.02.Words Frequency/Solution.swift new file mode 100644 index 0000000000000..786c533f40002 --- /dev/null +++ b/lcci/16.02.Words Frequency/Solution.swift @@ -0,0 +1,13 @@ +class WordsFrequency { + private var cnt: [String: Int] = [:] + + init(_ book: [String]) { + for word in book { + cnt[word, default: 0] += 1 + } + } + + func get(_ word: String) -> Int { + return cnt[word, default: 0] + } +}