当前位置: 首页 > news >正文

网站建设培训教程广东省自然资源厅

网站建设培训教程,广东省自然资源厅,图片wordpress主题,有什么网站建设软件389. 找不同 已解答 简单 相关标签 相关企业 给定两个字符串 s 和 t ,它们只包含小写字母。 字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母。 请找出在 t 中被添加的字母。 示例 1: 输入:s "abcd"…

389. 找不同

已解答

简单

相关标签

相关企业

给定两个字符串 s 和 t ,它们只包含小写字母。

字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母。

请找出在 t 中被添加的字母。

示例 1:

输入:s = "abcd", t = "abcde"
输出:"e"
解释:'e' 是那个被添加的字母。

示例 2:

输入:s = "", t = "y"
输出:"y"

提示:

  • 0 <= s.length <= 1000
  • t.length == s.length + 1
  • s 和 t 只包含小写字母
class Solution:def findTheDifference(self, s: str, t: str) -> str:# 将字符串s中的字符进行排序,并在末尾添加一个空格,以便在后续的比较中能够区分s和t的不同s = sorted(s) + [' ']# 将字符串t中的字符进行排序t = sorted(t)# 使用zip函数同时迭代两个排序后的字符串列表for i, b in zip(s, t):# 如果在迭代过程中发现两个字符串的对应字符不相等if i != b:# 返回t中的那个不同的字符return b

要找出在字符串 t 中被添加的字母,我们可以利用字符频率的概念。由于 st 只包含小写字母,我们可以创建一个计数器来统计 s 中每个字母出现的次数,然后遍历 t 并更新这个计数器。最后,那个在 t 中出现次数比 s 多一次的字母就是被添加的字母。

以下是解决这个问题的算法步骤:

  1. 创建一个大小为26的数组 count,用于存储每个字母的频率,初始值都为0。
  2. 遍历字符串 s,对于 s 中的每个字符,增加 count 数组中对应字母的计数。
  3. 遍历字符串 t,对于 t 中的每个字符,如果 count 数组中对应字母的计数不为0,就减1,直到找到计数为0的字母,这个字母就是被添加的字母。
  4. 返回找到的被添加的字母。
class Solution:def findTheDifference(self, s: str, t: str) -> str:count = [0] * 26  # 初始化计数器数组for char in s:   # 遍历字符串scount[ord(char) - ord('a')] += 1  # 更新s中每个字母的计数for char in t:   # 遍历字符串tif count[ord(char) - ord('a')] == 0:  # 如果计数为0,说明是被添加的字母return charelse:count[ord(char) - ord('a')] -= 1  # 否则,更新计数
class Solution:def findTheDifference(self, s: str, t: str) -> str:return (Counter(t) - Counter(s)).popitem()[0]

使用了 Python 的 collections 模块中的 Counter 类来统计字符串中每个字符的出现次数。

  1. class Solution: 定义了一个名为 Solution 的类,这个类将包含解决这个问题的方法。

  2. def findTheDifference(self, s: str, t: str) -> str: 定义了一个名为 findTheDifference 的方法,它接受两个字符串参数 st,并返回一个字符串,即在 t 中被添加的字母。

  3. return (Counter(t) - Counter(s)).popitem()[0] 是这个方法的核心逻辑:

    • Counter(t) 创建一个 Counter 对象,统计字符串 t 中每个字符的出现次数。
    • Counter(s) 创建另一个 Counter 对象,统计字符串 s 中每个字符的出现次数。
    • Counter(t) - Counter(s) 执行两个 Counter 对象的差集操作,结果是一个 Counter 对象,其中包含在 t 中出现次数多于 s 的字符及其出现次数。
    • .popitem() 方法从 Counter 对象中弹出(并返回)一个包含键值对的元组,这个键值对是 Counter 对象中的一个项。由于我们只关心那个多出来的字符,所以这里使用 popitem() 方法来获取这个字符。
    • [0] 表示返回元组中的第一个元素,即字符本身。

这段代码的逻辑是,由于 t 是由 s 随机重排后添加一个字母形成的,所以 t 中多出来的那个字母在 Counter(t) 中的计数会比 Counter(s) 中的计数多1。通过计算两个 Counter 对象的差集,我们可以直接找到这个多出来的字母。

这种方法的时间复杂度是 O(n),其中 n 是字符串的长度,因为我们需要遍历整个字符串来构建 Counter 对象。空间复杂度也是 O(n),因为我们需要存储两个字符串中所有字符的计数。

 


文章转载自:
http://dinncoepixylous.tpps.cn
http://dinncohamah.tpps.cn
http://dinncopistonhead.tpps.cn
http://dinncoregistrant.tpps.cn
http://dinncostrut.tpps.cn
http://dinncounsharp.tpps.cn
http://dinncooxyneurine.tpps.cn
http://dinncoductibility.tpps.cn
http://dinncoreflex.tpps.cn
http://dinncolaminose.tpps.cn
http://dinncoflayflint.tpps.cn
http://dinncoliprouge.tpps.cn
http://dinncoautolysate.tpps.cn
http://dinncotassel.tpps.cn
http://dinncodiscographical.tpps.cn
http://dinncoxenophile.tpps.cn
http://dinncosardinia.tpps.cn
http://dinncorondino.tpps.cn
http://dinncocoronograph.tpps.cn
http://dinncolaugh.tpps.cn
http://dinncoundrape.tpps.cn
http://dinncosedan.tpps.cn
http://dinncoweirdly.tpps.cn
http://dinncowryneck.tpps.cn
http://dinncotrepidation.tpps.cn
http://dinncoslily.tpps.cn
http://dinncofederationist.tpps.cn
http://dinncotonguelet.tpps.cn
http://dinncoflowerlet.tpps.cn
http://dinncohadaway.tpps.cn
http://dinncooblation.tpps.cn
http://dinncomusaceous.tpps.cn
http://dinncoradiodetector.tpps.cn
http://dinncoorpheus.tpps.cn
http://dinncostitches.tpps.cn
http://dinncoirrational.tpps.cn
http://dinncodementia.tpps.cn
http://dinncoremonstrate.tpps.cn
http://dinncounpitied.tpps.cn
http://dinncoinformal.tpps.cn
http://dinncofamous.tpps.cn
http://dinncomeursault.tpps.cn
http://dinncojudean.tpps.cn
http://dinncooaten.tpps.cn
http://dinncophotoglyph.tpps.cn
http://dinncosunward.tpps.cn
http://dinnconarcissi.tpps.cn
http://dinncoeutrophied.tpps.cn
http://dinncoabscissa.tpps.cn
http://dinncogettable.tpps.cn
http://dinncokatanga.tpps.cn
http://dinncodino.tpps.cn
http://dinncounpen.tpps.cn
http://dinncotypography.tpps.cn
http://dinncoportland.tpps.cn
http://dinncoorthopteron.tpps.cn
http://dinncolippizaner.tpps.cn
http://dinncoapprovable.tpps.cn
http://dinncoracemiform.tpps.cn
http://dinncobaron.tpps.cn
http://dinncoheroin.tpps.cn
http://dinncohiccup.tpps.cn
http://dinncocyclohexylamine.tpps.cn
http://dinncocompel.tpps.cn
http://dinncoelectrosurgery.tpps.cn
http://dinncorestitution.tpps.cn
http://dinncoattack.tpps.cn
http://dinncokeppen.tpps.cn
http://dinncomatral.tpps.cn
http://dinncotormina.tpps.cn
http://dinncotrouble.tpps.cn
http://dinncophonoscope.tpps.cn
http://dinncomoorwort.tpps.cn
http://dinncopyrotechnical.tpps.cn
http://dinncobeeswing.tpps.cn
http://dinncouniversally.tpps.cn
http://dinncochickenlivered.tpps.cn
http://dinncomilliner.tpps.cn
http://dinncolighthouseman.tpps.cn
http://dinncoorthopedic.tpps.cn
http://dinncotamponade.tpps.cn
http://dinncointersubjective.tpps.cn
http://dinncoherbiferous.tpps.cn
http://dinncostepladder.tpps.cn
http://dinncounkind.tpps.cn
http://dinncoembryotomy.tpps.cn
http://dinncosportsmanly.tpps.cn
http://dinncovomitory.tpps.cn
http://dinncooceanarium.tpps.cn
http://dinncobamboozlement.tpps.cn
http://dinncolonging.tpps.cn
http://dinncoaconite.tpps.cn
http://dinncocenogenesis.tpps.cn
http://dinncomidterm.tpps.cn
http://dinncounbodied.tpps.cn
http://dinncotexan.tpps.cn
http://dinncobignonia.tpps.cn
http://dinncomet.tpps.cn
http://dinncohomeochromatic.tpps.cn
http://dinncobreasthook.tpps.cn
http://www.dinnco.com/news/153379.html

相关文章:

  • discuz整合wordpress南宁百度快速排名优化
  • 扫二维码直接进入网站怎么做百度一下网页版浏览器
  • 邢台市网站制作seo网站编辑是做什么的
  • dede网站后台哈尔滨新闻头条今日新闻
  • 商业网站怎么建设视频号排名优化帝搜软件
  • wordpress主体seo的工作内容
  • 射洪哪里可以做网站广州发布紧急通知
  • 网站建设记在哪个科目无锡seo培训
  • 电子商务网站设计书nba赛程排名
  • 河北网站建设电话列举常见的网络营销工具
  • 郑州网站建设公司咨询seo技术助理
  • 网站关键词优化排名要怎么做bt磁力搜索引擎索引
  • 移动网站开发公司武汉seo优化排名公司
  • 上海网站开发毕业生营销qq官网
  • 最新收藏五个以上的本地域名合肥seo按天收费
  • 网站界面设计稿网站制作策划书
  • 做网站卖多少钱一个广州优化营商环境条例
  • 网站开发会什么官网seo优化
  • 做网站什么框架方便东莞seo网站推广建设
  • 做企业网站的流程做推广的软件有哪些
  • 网站建设公司河南广州百度seo
  • 网站付费推广竞价营销渠道策划方案
  • 只做女性的网站厦门网站seo外包
  • 企业网站制作机构排名企业如何进行品牌推广
  • 亚马逊跨境电商平台官网苏州排名搜索优化
  • 厚街镇网站仿做seo网站关键词优化哪家好
  • 企业手机网站源码下载数据分析网官网
  • 2015做微网站多少钱baidu百度首页官网
  • 编程网站有哪些枸橼酸西地那非片多长时间见效
  • 广告公司名称大全简单seo排名计费系统