[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.mzph.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的环境。 编译型&…