문제: https://leetcode.com/problems/most-common-word/submissions/
import Foundation
class Solution {
func mostCommonWord(_ paragraph: String, _ banned: [String]) -> String {
var dict = [String: Int]()
let words = paragraph.split(omittingEmptySubsequences: true, whereSeparator: { ", ".contains($0) })
for word in words {
let word = word.lowercased().filter { "abcdefghijklmnopqrstuvwxyz".contains($0) }
if !banned.contains(word) {
if dict[word] == nil {
dict[word] = 1
} else {
dict[word]! += 1
}
}
}
for word in dict.sorted(by: { $0.value > $1.value }) {
return word.key
}
return ""
}
}
'알고리즘' 카테고리의 다른 글
[Swift] LeetCode - 2. Add Two Numbers (0) | 2022.02.15 |
---|---|
[Swift] LeetCode - 49. Group Anagrams (0) | 2022.02.14 |
[Swift] LeetCode - 937. Reorder Data in Log Files (0) | 2022.02.14 |
[Swift] 프로그래머스 - 후보키 (0) | 2022.02.11 |
[Swift] 프로그래머스 - 다단계 칫솔 판매 (0) | 2022.02.11 |
댓글