[Swift]LeetCode884. 兩句話中的不常見單詞 | Uncommon Words from Two Sentences

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

We are given two sentences A and B.  (A sentence is a string of space separated words.  Each word consists only of lowercase letters.)git

A word is uncommon if it appears exactly once in one of the sentences, and does not appear in the other sentence.github

Return a list of all uncommon words. 微信

You may return the list in any order. app

Example 1:this

Input: A = "this apple is sweet", B = "this apple is sour" Output: ["sweet","sour"] 

Example 2:spa

Input: A = "apple apple", B = "banana" Output: ["banana"] 

Note:code

  1. 0 <= A.length <= 200
  2. 0 <= B.length <= 200
  3. A and B both contain only spaces and lowercase letters.

給定兩個句子 A 和 B 。 (句子是一串由空格分隔的單詞。每一個單詞僅由小寫字母組成。)component

若是一個單詞在其中一個句子中只出現一次,在另外一個句子中卻沒有出現,那麼這個單詞就是不常見的。htm

返回全部不經常使用單詞的列表。

您能夠按任何順序返回列表。

 示例 1:

輸入:A = "this apple is sweet", B = "this apple is sour"
輸出:["sweet","sour"]

示例 2:

輸入:A = "apple apple", B = "banana"
輸出:["banana"] 

提示:

  1. 0 <= A.length <= 200
  2. 0 <= B.length <= 200
  3. A 和 B 都只包含空格和小寫字母。

8ms
 1 class Solution {
 2     func uncommonFromSentences(_ A: String, _ B: String) -> [String] {
 3         var occurences = [String: Int]()
 4         let addSubstringToOccurences = { (substring: Substring) -> Void in
 5             let word = String(substring)
 6             occurences[word] = (occurences[word] ?? 0) + 1
 7         }
 8 
 9         A.split(separator: " ").forEach(addSubstringToOccurences)
10         B.split(separator: " ").forEach(addSubstringToOccurences)
11 
12         return Array(occurences.filter { $1 == 1 }.keys)
13     }
14 }

12ms

 1 class Solution {
 2     func uncommonFromSentences(_ A: String, _ B: String) -> [String] {
 3         var result : [String] = []
 4         var DictA : [String:Int] = [:]
 5         var DictB : [String:Int] = [:]
 6         var wordsA = A.components(separatedBy: " ")
 7         var wordsB = B.components(separatedBy: " ")
 8         
 9         for word in wordsA {
10             if var count = DictA[word] {
11                 DictA[word] = count + 1
12             } else {
13                 DictA[word] = 1
14             }
15         }
16         
17         for word in wordsB {
18             if var count = DictB[word] {
19                 DictB[word] = count + 1
20             } else {
21                 DictB[word] = 1
22             }
23         }
24         
25         for key in DictA.keys {
26             if DictA[key] == 1 && DictB[key] == nil {
27                 result.append(key)
28             }
29         }
30         
31         for key in DictB.keys {
32             if DictB[key] == 1 && DictA[key] == nil {
33                 result.append(key)
34             }
35         }
36         
37         return result
38     }
39 }

16ms

 1 class Solution {
 2    func uncommonFromSentences(_ A: String, _ B: String) -> [String] {
 3         let str = A + " " + B
 4         var dic = Dictionary<String,Int>()
 5         var result = [String]()
 6         let subStr = str.split(separator: " ")
 7         let subs = subStr.map { String($0)}
 8         for sub in subs {
 9             if nil == dic[sub] {
10                 dic[sub] = 1
11             } else {
12                 dic[sub] = dic[sub]! + 1
13             }
14         }
15         dic.filter { (arg0) -> Bool in
16             
17             let (key, value) = arg0
18             if value == 1 {
19                 result.append(key)
20                 return true
21             } else {
22                 return false
23             }
24         }
25         return result
26     }
27 }

Runtime: 20 ms
Memory Usage: 20.3 MB
 1 class Solution {
 2     func uncommonFromSentences(_ A: String, _ B: String) -> [String] {
 3         var count:[String:Int] = [String:Int]()
 4         var arr:[String] = (A + " " + B).components(separatedBy:" ")
 5         for w in arr
 6         {
 7             count[w,default:0] += 1
 8         }
 9         var res:[String] = [String]()
10         for w in count.keys
11         {
12             if count[w,default:0] == 1
13             {
14                 res.append(w)
15             }
16         }
17         return res
18     }
19 }

24ms

 1 class Solution {
 2     func uncommonFromSentences(_ A: String, _ B: String) -> [String] {
 3         
 4         var wordCountDict: [Substring : Int] = [:]
 5         
 6         for word in A.split(separator: " ") {
 7              wordCountDict[word] = (wordCountDict[word] ?? 0) + 1
 8         }
 9         
10         for word in B.split(separator: " ") {
11             wordCountDict[word] = (wordCountDict[word] ?? 0) + 1
12         }
13         
14         var answer: [String] = []
15         
16         for (word, count) in wordCountDict {
17             if count == 1 {
18                 answer.append(String(word))
19             }
20         }
21         
22         return answer
23     }
24 }

32ms

 1 class Solution {
 2     func uncommonFromSentences(_ A: String, _ B: String) -> [String] {
 3         let words = A.split(separator: " ").map { String($0) } + B.split(separator: " ").map { String($0) }
 4         var countDict = [String: Int]()
 5 
 6         for word in words {
 7             countDict[word] = (countDict[word] ?? 0) + 1
 8         }
 9 
10         return countDict.filter { $0.value == 1 }.map { $0.key }
11     }
12 }

36ms

 1 class Solution {
 2     func uncommonFromSentences(_ A: String, _ B: String) -> [String] {
 3         var result: [String] = []
 4         var count: [Substring: Int] = [:]
 5         let arrayA = A.split(separator: " ")
 6         let arrayB = B.split(separator: " ")
 7         for s in arrayA {
 8             if let value = count[s] {
 9                 count[s] = value + 1
10             } else {
11                 count[s] = 1
12             }
13         }
14         for s in arrayB {
15             if let value = count[s] {
16                 count[s] = value + 1
17             } else {
18                 count[s] = 1
19             }
20         }
21         for (key,value) in count {
22             if value == 1 {
23                 result.append(String(key))
24             }
25         }
26         return result
27     }
28 }
相關文章
相關標籤/搜索