[Swift]LeetCode819. 最多見的單詞 | Most Common Word

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-fwcwgeqf-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

Given a paragraph and a list of banned words, return the most frequent word that is not in the list of banned words.  It is guaranteed there is at least one word that isn't banned, and that the answer is unique.git

Words in the list of banned words are given in lowercase, and free of punctuation.  Words in the paragraph are not case sensitive.  The answer is in lowercase. github

Example:微信

Input: 
paragraph = "Bob hit a ball, the hit BALL flew far after it was hit."
banned = ["hit"]
Output: "ball"
Explanation: 
"hit" occurs 3 times, but it is a banned word.
"ball" occurs twice (and no other word does), so it is the most frequent non-banned word in the paragraph. 
Note that words in the paragraph are not case sensitive,
that punctuation is ignored (even if adjacent to words, such as "ball,"), 
and that "hit" isn't the answer even though it occurs more because it is banned. 

Note:app

  • 1 <= paragraph.length <= 1000.
  • 1 <= banned.length <= 100.
  • 1 <= banned[i].length <= 10.
  • The answer is unique, and written in lowercase (even if its occurrences in paragraph may have uppercase symbols, and even if it is a proper noun.)
  • paragraph only consists of letters, spaces, or the punctuation symbols !?',;.
  • There are no hyphens or hyphenated words.
  • Words only consist of letters, never apostrophes or other punctuation symbols.

給定一個段落 (paragraph) 和一個禁用單詞列表 (banned)。返回出現次數最多,同時不在禁用列表中的單詞。題目保證至少有一個詞不在禁用列表中,並且答案惟一。post

禁用列表中的單詞用小寫字母表示,不含標點符號。段落中的單詞不區分大小寫。答案都是小寫字母。 spa

示例:code

輸入: 
paragraph = "Bob hit a ball, the hit BALL flew far after it was hit."
banned = ["hit"]
輸出: "ball"
解釋: 
"hit" 出現了3次,但它是一個禁用的單詞。
"ball" 出現了2次 (同時沒有其餘單詞出現2次),因此它是段落裏出現次數最多的,且不在禁用列表中的單詞。 
注意,全部這些單詞在段落裏不區分大小寫,標點符號須要忽略(即便是緊挨着單詞也忽略, 好比 "ball,"), 
"hit"不是最終的答案,雖然它出現次數更多,但它在禁用單詞列表中。 

說明:component

  • 1 <= 段落長度 <= 1000.
  • 1 <= 禁用單詞個數 <= 100.
  • 1 <= 禁用單詞長度 <= 10.
  • 答案是惟一的, 且都是小寫字母 (即便在 paragraph 裏是大寫的,即便是一些特定的名詞,答案都是小寫的。)
  • paragraph 只包含字母、空格和下列標點符號!?',;.
  • 不存在沒有連字符或者帶有連字符的單詞。
  • 單詞裏只包含字母,不會出現省略號或者其餘標點符號。

24msorm

 1 class Solution {
 2     func mostCommonWord(_ paragraph: String, _ banned: [String]) -> String {
 3       let paraArray = paragraph.split { (c) -> Bool in
 4             let ascii = c.unicodeScalars.first!.value
 5             return ascii < 65 || (ascii > 90 && ascii < 97) || ascii > 122
 6         }
 7         
 8         let banned: Set<String> = Set(banned)
 9         
10         var map: [String: Int] = [:]
11         for word in paraArray {
12             map[String(word).lowercased(), default: 0] += 1
13         }
14         
15         for (key, _) in map.sorted(by: { (word1, word2) -> Bool in
16             return word1.value > word2.value
17         }) {
18             if !banned.contains(key) {
19                 return key
20             }
21         }
22         
23         return ""
24     }
25 }

36ms

 1 class Solution {
 2     func mostCommonWord(_ paragraph: String, _ banned: [String]) -> String {
 3         var paragraph = Array(paragraph.lowercased())
 4         for i in 0..<paragraph.count {
 5             if paragraph[i] < "a" || paragraph[i] > "z" {
 6                 paragraph[i] = " "
 7             }
 8         }
 9         let words = String(paragraph).split(separator: " ").map{String($0)}
10         var result = ""
11         var max = 0
12         var map = [String:Int]()
13         for word in words {
14             if !banned.contains(word) {
15                 map[word] = (map[word] ?? 0) + 1
16                 if map[word]! > max {
17                     max = map[word]!
18                     result = word
19                 }
20             }
21         }
22         return result
23     }
24 }

44ms

 1 class Solution {
 2     func mostCommonWord(_ paragraph: String, _ banned: [String]) -> String {
 3         var paras = Array(paragraph.lowercased())
 4         for i in paras.indices {
 5             if paras[i] < "a" || paras[i] > "z" { paras[i] = " " }
 6         }
 7         let words = String(paras).components(separatedBy: " ")
 8         var res = ""
 9         var mx = 0
10         var m = [String: Int]()
11         for word in words where !word.isEmpty {
12             if !banned.contains(word) {
13                 m[word, default: 0] += 1
14                 if m[word]! > mx {
15                     mx = m[word]!
16                     res = word
17                 }
18             }
19         }
20         return res
21     }
22 }

48ms

 1 class Solution {
 2     func mostCommonWord(_ paragraph: String, _ banned: [String]) -> String {
 3         var counts: [String: Int] = [:]
 4         
 5         // let s = Set(banned)
 6         
 7         for w in paragraph.lowercased().components(separatedBy: .punctuationCharacters).joined(separator: " ").split(separator: " ") {
 8 
 9             counts[String(w), default: 0] += 1
10             // if counts[String(w)] != nil {
11             //     counts[String(w)]! += 1
12             // } else {
13             //     counts[String(w)] = 1
14             // }
15         }
16         
17         // let k = counts.filter({ !s.contains($0.key) }).max(by: { $1.value > $0.value })!.key
18         
19         return counts.lazy.filter({ !Set(banned).contains($0.key) }).max(by: { $1.value > $0.value })!.key
20         // return counts.max(by: { $1.value > $0.value })!.key    
21     }
22 }

52ms

 1 class Solution {
 2     func mostCommonWord(_ paragraph: String, _ banned: [String]) -> String {
 3         var wordCount = [String: Int]()
 4         var bannedWords = Set(banned)
 5         for substring in paragraph.replacingOccurrences(of: ",", with: " ").split(separator: " ") {
 6             let word = substring.lowercased().trimmingCharacters(in: .punctuationCharacters)
 7             if !bannedWords.contains(word) {
 8                 wordCount[word] = wordCount[word, default: 0] + 1
 9             }
10         }
11         return wordCount.max { a, b in a.value < b.value}!.key
12     }
13 }

60ms

 1 class Solution {
 2     func mostCommonWord(_ paragraph: String, _ banned: [String]) -> String {
 3         var bannedDict = Set<String>()
 4         banned.forEach{bannedDict.insert($0.lowercased())}
 5         var paraComponents = paragraph.lowercased().components(separatedBy: .punctuationCharacters).joined(separator: " ").components(separatedBy: " ").filter{$0 != ""}
 6         var memo = [String : Int]()
 7         var count = 0
 8         var answer = ""
 9         
10         for comp in paraComponents{
11             if bannedDict.contains(comp){
12                 continue
13             }
14             if memo[comp] != nil{
15                 memo[comp] = memo[comp]! + 1
16             }else{
17                 memo[comp] = 1
18             }
19             
20             if memo[comp]! > count {
21                 count = memo[comp]!
22                 answer = comp
23             }
24         }
25         
26         return answer
27     }
28 }

Runtime: 60 ms
Memory Usage: 21 MB
 1 class Solution {
 2     func mostCommonWord(_ paragraph: String, _ banned: [String]) -> String {
 3         
 4         let bannedSet = Set(banned)
 5         
 6         let wordArray = paragraph
 7             .lowercased()
 8             .components(separatedBy: CharacterSet(["!", "?", "'", ",", ";", ".", " "]))
 9             .filter { !$0.isEmpty && !bannedSet.contains($0) }
10         
11         var wordFrequencyDict: [String : UInt] = [:]
12         
13         for word in wordArray {
14             wordFrequencyDict[word] = wordFrequencyDict[word, default: 0] + 1
15         }
16         
17         return wordFrequencyDict.max { $0.value < $1.value }!.key
18     }
19 }

64ms

 1 class Solution {
 2     func mostCommonWord(_ paragraph: String,
 3                         _ banned: [String]) -> String {
 4         // lower case
 5         let paragraph = paragraph.lowercased()
 6         let banned = Set(banned)
 7         // store the words
 8         var onHold: String? = nil
 9         var storage: [String: Int] = [:]
10         for char in paragraph {
11             if char.unicodeScalars.allSatisfy(CharacterSet.alphanumerics.contains(_:)) {
12                 if onHold == nil {
13                     onHold = ""
14                 }
15                 onHold!.append(char)
16             } else {
17                 guard onHold != nil else { continue }
18                 storage[onHold!, default: 0] += 1
19                 onHold = nil
20             }
21         }
22         if !(onHold?.isEmpty ?? true) {
23             storage[onHold!, default: 0] += 1
24         }
25         // find the max
26         var max = -1
27         var maxKey = ""
28         for (key, value) in storage {
29             if value > max && !banned.contains(key) {
30                 maxKey = key
31                 max = value
32             }
33         }
34         return maxKey
35     }
36 }

68ms

 1 class Solution {
 2     func mostCommonWord(_ paragraph: String, _ banned: [String]) -> String {
 3         guard paragraph.count > 0 else {
 4             return paragraph
 5         }
 6         let arr = paragraph.components(separatedBy: CharacterSet.punctuationCharacters).joined(separator:" ").components(separatedBy: .whitespaces).filter({!$0.isEmpty})
 7         var map = [String:Int]()
 8         var max = Int.min
 9         var res = ""
10         for string in arr {
11             var st = string.lowercased()
12             if banned.contains(st) {continue}
13             if let count = map[st] {
14                 map[st] = count + 1
15             }
16             else{
17                 map[st] = 1
18             }
19             if map[st]! > max {
20                 max = map[st]!
21                 res = st
22             }
23         }
24         return res
25     }
26 }

72ms

 1 class Solution {
 2     func mostCommonWord(_ paragraph: String, _ banned: [String]) -> String {
 3         var lower = paragraph.lowercased()
 4         var para = splitString(lower)
 5         var dict = getWordCountDict(para, banned: banned)
 6         var maxCount = 0
 7         var result = ""
 8         for (s, c) in dict {
 9             if c > maxCount {
10                 maxCount = c
11                 result = s
12             }
13         }
14         return result
15     }
16     
17     func splitString(_ s: String) -> [String] {
18         return s.components(separatedBy:CharacterSet(charactersIn:",. !?;:'"))
19     }
20     
21     func getWordCountDict(_ array: [String], banned: [String]) -> [String: Int] {
22         var result = [String: Int]()
23         for w in array {
24             if w.length == 0 {
25                 continue
26             }
27             if banned.contains(w) {
28                 continue
29             }
30             if let v = result[w] {
31                 let nv = v + 1
32                 result[w] = nv
33             } else {
34                 result[w] = 1
35             }
36         }
37         return result
38     }    
39 }

84ms

 1 class Solution {
 2     func mostCommonWord(_ paragraph: String, _ banned: [String]) -> String {
 3         var counts: [String: Int] = [:]
 4         
 5         for w in  paragraph.components(separatedBy: .punctuationCharacters).joined(separator: " ").lowercased().split(separator: " ") where !Set(banned).contains(String(w)) {
 6             let sw = String(w)
 7             
 8             if counts[sw] != nil {
 9                 counts[sw]! += 1
10             } else {
11                 counts[sw] = 1
12             }
13         }
14         
15         return counts.max(by: { $1.value > $0.value })!.key    
16     }
17 }

92ms

 1 class Solution {
 2     func mostCommonWord(_ paragraph: String, _ banned: [String]) -> String {        
 3         let formattedPara = formattedParagraph(paragraph)
 4         var bannedWords = Set<String>()
 5         print(formattedPara)
 6         //throw them into a set
 7         for word in  banned {
 8             bannedWords.insert(word)
 9         }
10         var map = [String: Int]()
11         //now go through each word and if it's not in bannedWords, push to a dict
12         //with it's count
13         for word in formattedPara.components(separatedBy: " ") {
14             if !bannedWords.contains(word) && !word.isEmpty {
15                 map[word, default: 0] += 1
16             }
17         }        
18         //now sort the dict values in decreasing order        
19         var sortedTupleArr = map.sorted {$0.1 > $1.1}
20         return sortedTupleArr[0].0        
21     }
22     
23     private func formattedParagraph( _ para: String) -> String {
24         var para = para.lowercased()
25         let pattern = "[^A-Za-z]"
26         let regex = try! NSRegularExpression(pattern: pattern)
27         let range = NSRange(location: 0, length: para.count)
28         let para1 = regex.stringByReplacingMatches(in: para, options: .reportProgress,
29                             range: range, withTemplate: " ")
30         //print(para1)
31         return para1
32     }
33 }

96ms

 1 class Solution {
 2     func mostCommonWord(_ paragraph: String, _ banned: [String]) -> String {
 3         
 4         let lowercaseParagraph = paragraph.lowercased()
 5         let components = lowercaseParagraph.components(separatedBy: CharacterSet.letters.inverted).filter({ $0 != "" })
 6         let set = NSCountedSet(array:components)
 7         let bannedSet = Set(banned)
 8         
 9         var max = 0   
10         var answer = ""
11                                                                                                            
12         for word in set{
13             if let answerWord = word as? String{
14                 let repeatCount = set.count(for:answerWord)
15                 
16                 if ( repeatCount > max && !bannedSet.contains(answerWord)){
17                     max = repeatCount
18                     answer = answerWord
19                 }
20             }
21         }
22                                                                                                            
23        return answer
24     }
25 }

100ms

 1 class Solution {
 2     func mostCommonWord(_ paragraph: String, _ banned: [String]) -> String {
 3         let letters : Set<Character> =  Set("abcdefghijklmnopqrstuvwxyz")
 4         let w = paragraph.replacingOccurrences(of: "[!?',;.]", with: " ", options: .regularExpression, range: nil)
 5         var banned = Set(banned)
 6         let words = w.components(separatedBy: " ").filter { $0.count > 0 }.map { $0.lowercased().filter { letters.contains($0) } }.filter { !banned.contains($0) }
 7         var count: [String: Int] = [:]
 8         var maxC = 1
 9         var res = words[0]
10         for word in words {
11             if let some = count[word] {
12                 count[word] = some + 1
13                 if (some + 1) > maxC {
14                     maxC = some + 1
15                     res = word
16                 }
17             } else {
18                 count[word] = 1
19             }
20         }
21         return res
22     }
23 }

104ms

 1 class Solution {
 2     func mostCommonWord(_ paragraph: String, _ banned: [String]) -> String {
 3     let nonLowercaseSet = CharacterSet.lowercaseLetters.inverted
 4         let words = paragraph.lowercased().components(separatedBy: nonLowercaseSet).filter { !banned.contains($0.lowercased()) && !$0.isEmpty}
 5         
 6         let set = NSCountedSet(array: words)
 7         let sorted = set.allObjects.sorted { (first, second) -> Bool in
 8             set.count(for: first) > set.count(for: second)
 9         }
10         
11         return sorted.first! as! String
12     }
13 }

 1 class Solution {
 2     func mostCommonWord(_ paragraph: String, _ banned: [String]) -> String {
 3         let bannedSet = Set(banned)
 4         var wordFrequency = [String: Int]()
 5         
 6         var word = ""
 7         var maxFrequencyWord = ""
 8         var maxFrequency = 0
 9         
10         func updateWordFrequency() {
11             if !word.isEmpty && !bannedSet.contains(word) {
12                 wordFrequency[word] =  (wordFrequency[word] ?? 0) + 1
13                 if wordFrequency[word]! >= maxFrequency {
14                     maxFrequency = wordFrequency[word]!
15                     maxFrequencyWord = word
16                 }
17             }
18         }
19         
20         for character in paragraph.lowercased() {
21             if character >= Character("a") && character <= Character("z") {
22                 word.append(character)   
23             } else {
24                 // This is new word, word is not empty
25                 updateWordFrequency()
26                 word = ""
27             }
28         }
29         updateWordFrequency()
30         return maxFrequencyWord
31     }
32 }

 1 class Solution {
 2     func mostCommonWord(_ paragraph: String, _ banned: [String]) -> String {
 3         var word = ""
 4         var freq = [String: Int]()
 5         for c in paragraph {
 6             if !c.isAlpha() {
 7                 if word.count > 0 {
 8                     word = word.lowercased()
 9                     if let k = freq[word] {
10                         freq[word] = k + 1
11                     } else {
12                         freq[word] = 1
13                     }
14                 }
15                 word = ""
16                 continue
17             }
18             word.append(c)
19         }
20         if word.count > 0 {
21             word = word.lowercased()
22             if let k = freq[word] {
23                 freq[word] = k + 1
24             } else {
25                 freq[word] = 1
26             }
27         }
28         
29         let sortedFreq = freq.sorted(by: {$0.value > $1.value})
30         for item in sortedFreq {
31             if !banned.contains(item.key) {
32                 return item.key
33             }
34         }
35         return ""
36     }
37 }
38 
39 extension Character {
40     var ascii: Int {
41         let charVal = self.unicodeScalars
42         if let c = charVal.first {
43             return Int(c.value)
44         }
45         return -1
46     }
47     func isAlpha() -> Bool {
48         return ascii >= 97 && ascii <= 122 || ascii >= 65 && ascii <= 90
49     }    
50 }
相關文章
相關標籤/搜索