• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

[Swift]LeetCode884.两句话中的不常见单词|UncommonWordsfromTwoSentences

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: https://www.cnblogs.com/strengthen/p/10603493.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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 = 

Example 2:

Input: A =  

Note:

  1. 0 <= A.length <= 200
  2. 0 <= B.length <= 200
  3. 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"] 

提示:

  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 }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap