[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:

  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 }

?

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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/387148.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/387148.shtml
英文地址,請注明出處:http://en.pswp.cn/news/387148.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

微信公眾號 語音錄音jssdk

1.開發流程 如果開發的是普通的展示性頁面&#xff0c;就和開發普通的頁面沒有區別&#xff0c;不過這里要用到設備&#xff08;手機&#xff09;的錄音功能&#xff0c;就需要調用微信app的錄音接口&#xff0c;需要使用微信jssdk。 使用微信jssdk&#xff1a;微信JS-SDK說明文…

iview table 方法若干

新增默認選中1. _checked字段增加2. 給data項設置特殊 key _checked: true2.0 多選框樣式錯亂&#xff0c;默認選中問題1. 修改為元素checkbox 樣式大概調整2. 如果樣式不好看 可以自行修改或者使用其他組件ui checkboxAPI props 屬性說明類型items顯示的結構化數據Arraycolumn…

05 MapReduce應用案例01

1、單詞計數 在一定程度上反映了MapReduce設計的初衷--對日志文件進行分析。 public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable>{//該方法循環調用&#xff0c;從文件的split中讀取每行調用一次&#xff0c;把該行所在的下標為key&a…

ios高級開發之多線程(一)

1.概念&#xff1a; 多線程&#xff08;multithreading&#xff09;到底是什么呢&#xff0c;它是指在軟件或者硬件上實現多個線程并發執行的技術。具有多線程能力的計算機因有硬件的支持&#xff0c;而能夠在同一時間執行多個線程&#xff0c;進而提升整體處理性能。在一個程序…

v-if的簡單應用

<span v-if"item.status0"> 項目狀態&#xff1a;未提交 </span> <span v-if"item.status1"> 項目狀態&#xff1a;審批中 </span> <span v-if"item.status2"> 項目狀態&#xff1a;審批退回 </span> <s…

05 MapReduce應用案例02

6、統計每個月份中&#xff0c;最高的三個溫度。 輸入格式&#xff1a;年月日 空格 時分秒 TAB 溫度 inputfile: 1949-10-01 14:21:02 34c 1949-10-02 14:01:02 36c 1950-01-01 11:21:02 32c 1950-10-01 12:21:02 37c 1951-12-01 12:21:02 23c 1950-10-…

05 MapReduce應用案例03

8、PageRank Page-rank源于Google&#xff0c;用于衡量特定網頁相對于搜索引擎索引中的其他網頁而言的重要程度。 Page-rank實現了將鏈接價值概念作為排名因素。 算法原理 – 入鏈 投票 ? Page-rank 讓鏈接來“ 投票 “ ,到一個頁面的超鏈接相當于對該頁投一票。 – 入…

利用微信的weui框架上傳、預覽和刪除圖片

jQuery WeUI 是專為微信公眾賬號開發而設計的一個框架&#xff0c;jQuery WeUI的官網&#xff1a;http://jqweui.com/ 需求&#xff1a;需要在微信公眾號網頁添加上傳圖片功能 技術選型&#xff1a;實現上傳圖片功能可選百度的WebUploader、餓了么的Element和微信的jQuery WeUI…

【轉】Java Socket編程基礎及深入講解

原文&#xff1a;https://www.cnblogs.com/yiwangzhibujian/p/7107785.html#q2.3.3 Socket是Java網絡編程的基礎&#xff0c;了解還是有好處的&#xff0c; 這篇文章主要講解Socket的基礎編程。Socket用在哪呢&#xff0c;主要用在進程間&#xff0c;網絡間通信。本篇比較長&am…

使用 vue-i18n 切換中英文

使用 vue-i18n 切換中英文vue-i18n 倉庫地址&#xff1a;https://github.com/kazupon/vue-i18n兼容性&#xff1a;支持 Vue.js 2.x 以上版本安裝方法&#xff1a;&#xff08;此處只演示 npm&#xff09;npm install vue-i18n使用方法&#xff1a;1、在 main.js 中引入 vue-i18…

ZooKeeper數據模型

Zookeeper的數據模型 層次化的目錄結構&#xff0c;命名符合常規文件系統規范&#xff08;Linux&#xff09; 每個節點在zookeeper中叫做znode,并且其有一個唯一的路徑標識 節點Znode可以包含數據和子節點(即子目錄)&#xff0c;但是EPHEMERAL類型的節點不能有子節點 Znod…

堆疊條形圖

堆疊條形圖 import pandas as pd import numpy as np import matplotlib.pyplot as plt import matplotlib as mpl import matplotlib.dates as mdates#解決能顯示中文 mpl.rcParams[font.sans-serif][SimHei] #指定默認字體 SimHei為黑體 mpl.rcParams[axes.unicode_minus]Fal…

spring boot 服務器常用

ps aux|grep tgcwll /opt/nginx/html sudo cp -r /tmp/tgcw/dist/* /opt/nginx/html/design sudo cp -r /tmp/tgcw/dist/* /opt/nginx/html springboot 啟動nohup java -jar tgcw-service-usermanagement-0.0.1-SNAPSHOT.jar --spring.profiles.activedemo > /dev/null 2&g…

PHP數組 轉 對象/對象 轉 數組

/*** 數組 轉 對象** param array $arr 數組* return object*/ function array_to_object($arr) {if (gettype($arr) ! array) {return;}foreach ($arr as $k > $v) {if (gettype($v) array || getType($v) object) {$arr[$k] (object)array_to_object($v);}}return (obj…

ZooKeeper編程01--RMI服務的多服務器管理

服務器端與客戶端都要用到&#xff1a; public interface ZkInfo {String ZK_CONNECTION_STRING "192.168.1.201:2181,192.168.1.202:2181,192.168.1.203:2181";int ZK_SESSION_TIMEOUT 5000;String ZK_REGISTRY_PATH "/registry";String ZK_PROVIDER_…

org.activiti.engine.ActivitiOptimisticLockingException updated by another transaction concurrently

org.activiti.engine.ActivitiOptimisticLockingException: Task[id5905010, name審核(市場部)] was updated by another transaction concurrentlyat org.activiti.engine.impl.db.DbSqlSession.flushUpdates(DbSqlSession.java:872)at org.activiti.engine.impl.db.DbSqlSess…

DataTable不能通過已刪除的行訪問該行的信息解決方法

使用dt.Rows[0]["name", DataRowVersion.Original]可以獲取轉載于:https://www.cnblogs.com/heyiping/p/10616640.html

ZooKeeper編程02--多線程的分佈式鎖

面向過程版&#xff1a; package distributedLockProcess;import java.util.Collections; import java.util.List; import java.util.concurrent.CountDownLatch;import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; import org.apache.zoo…

01 Python變量和數據類型

Python變量和數據類型 1 數據類型 計算機&#xff0c;顧名思義就是可以做數學計算的機器&#xff0c;因此&#xff0c;計算機程序理所當然也可以處理各種數值。 但是&#xff0c;計算機能處理的遠不止數值&#xff0c;還可以處理文本、圖形、音頻、視頻、網頁等各種各樣的數…

初識Python-1

1&#xff0c;計算機基礎。 2&#xff0c;python歷史。 宏觀上&#xff1a;python2 與 python3 區別&#xff1a; python2 源碼不標準&#xff0c;混亂&#xff0c;重復代碼太多&#xff0c; python3 統一 標準&#xff0c;去除重復代碼。 3&#xff0c;python的環境。 編譯型&…