日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

[Swift]LeetCode884. 两句话中的不常见单词 | Uncommon Words from Two Sentences

發布時間:2023/11/30 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [Swift]LeetCode884. 两句话中的不常见单词 | Uncommon Words from Two Sentences 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
?微信公眾號:山青詠芝(shanqingyongzhi)
?博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
?GitHub地址:https://github.com/strengthen/LeetCode
?原文地址:?https://www.cnblogs.com/strengthen/p/10603493.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.)

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

Return a list of all uncommon words.?

You may return the list in any order.?

Example 1:

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

Example 2:

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

Note:

  • 0 <= A.length <= 200
  • 0 <= B.length <= 200
  • A?and?B?both contain only spaces and lowercase letters.

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

    如果一個單詞在其中一個句子中只出現一次,在另一個句子中卻沒有出現,那么這個單詞就是不常見的。

    返回所有不常用單詞的列表。

    您可以按任何順序返回列表。

    ?示例 1:

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

    示例?2:

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

    提示:

  • 0 <= A.length <= 200
  • 0 <= B.length <= 200
  • 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 }

    ?

    轉載于:https://www.cnblogs.com/strengthen/p/10603493.html

    總結

    以上是生活随笔為你收集整理的[Swift]LeetCode884. 两句话中的不常见单词 | Uncommon Words from Two Sentences的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。