389. Find the Difference [easy] (Python)

python 找不同 Find the Difference 给定两个字符串 s 和 t,它们只包含小写字母。字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母。请找出在 t 中被添加的字母。示例:输入: s = "abcd" t = "abcde"输出: e解释: 'e' 是那个被添加的字母。class Solution(object): def findTheDifference(self, s, t): """ ... 阅读详情

题目链接

https://leetcode.com/problems/find-the-difference/

题目原文

Given two strings s and t which consist of only lowercase letters.
String t is generated by random shuffling string s and then add one more letter at a random position.
Find the letter that was added in t.

Example:

Input:
s = "abcd"
t = "abcde"

Output:
e

Explanation:
'e' is the letter that was added.

题目翻译

给定两个由小写字母构成的字符串s和t,t是由s中的所有字母随机打乱后在任一位置插入一个新字母构成的字符串。请在t中找出新加入的字母。

思路方法

思路一

分别统计s和t中每个字母出现的次数,不一样的即为所求。为了减少计算量,可以先遍历s,用数组统计26个字母出现的次数;再遍历t,在刚才的数组基础上对出现的字母次数减一,次数出现负数的字母即为所求。

代码

class Solution(object):
    def findTheDifference(self, s, t):
        """
        :type s: str
        :type t: str
        :rtype: str
        """
        letters = [0] * 26
        for c in s:
            letters[ord(c) - 97] += 1
        for c in t:
            letters[ord(c) - 97] -= 1
            if letters[ord(c) - 97] < 0:
                return c

思路二

类似上面的思路,用dict实现也比较方便。

代码

class Solution(object):
    def findTheDifference(self, s, t):
        """
        :type s: str
        :type t: str
        :rtype: str
        """
        letters = {}
        for c in s:
            letters[c] = letters[c] + 1 if c in letters else 1
        for c in t:
            if c not in letters:
                return c
            letters[c] -= 1
            if letters[c] < 0:
                return c

思路三

考虑到t是由s添加一个字母得到,那么这个添加的字母在s和t中出现的总次数一定是唯一的一个奇数。基于这个想法,用异或操作得到该字母。

代码

class Solution(object):
    def findTheDifference(self, s, t):
        """
        :type s: str
        :type t: str
        :rtype: str
        """
        return chr(reduce(operator.xor, map(ord, s + t)))

思路四

类似思路三,除了用异或操作,还可以用加减法找到该字母。

代码

class Solution(object):
    def findTheDifference(self, s, t):
        """
        :type s: str
        :type t: str
        :rtype: str
        """
        res = ord(t[-1])
        for i in xrange(len(s)):
            res = res + ord(t[i]) - ord(s[i])
        return chr(res)

PS: 新手刷LeetCode,新手写博客,写错了或者写的不清楚还请帮忙指出,谢谢!
转载请注明:http://blog.csdn.net/coder_orz/article/details/52387709

告别迷茫!用ETAS RTA-OS配置你的第一个AUTOSAR OS任务(含VRTA虚拟ECU实战) 本文详细介绍了如何使用ETAS RTA-OS工具链在VRTA虚拟ECU上配置和运行第一个AUTOSAR OS任务。通过从XML配置到任务调度的全流程实战,帮助开发者快速掌握嵌入式操作系统开发的核心步骤,特别适合刚接触AUTOSAR的开发者快速上手。 阅读详情

相关推荐

Vue3项目实战:CKEditor5富文本编辑器从安装到隐藏不需要的功能

本文详细介绍了在Vue3项目中集成并深度定制CKEditor5富文本编辑器的完整流程。通过分析其模块化架构,指导开发者从标准构建出发,移除图片、表格等冗余插件,创建专属轻量构建,从而显著减小打包体积并提升用户体验。文章结合实战代码,涵盖了环境搭建、自定义构建配置、高级优化技巧等关键步骤。

weixin_29016833的博客 295

Leetcode 389. Find the Difference (Python)

You are given two strings and .String is generated by random shuffling string and then add one more letter at a random position.Return the letter that was added to .Example 1: Input: s = "abcd", t = "abcde" Output: "e" Expl

weixin_58080442的博客 293

告别安装报错!保姆级教程:用阿狸狗破戒大师V3.2.6搞定Cadence 17.4完整安装与补丁

本文提供Cadence 17.4的完整安装与破解教程,特别针对国内用户常见问题优化。从前期准备、镜像处理到阿狸狗破戒大师V3.2.6的智能破解,再到补丁管理与性能调优,详细指导每一步操作,确保一次性安装成功并提升设计效率。

weixin_30632089的博客 866

leetcode之389. Find the Difference(C++解法)

原题: Given two strings s and t which consist of only lowercase letters.String t is generated by random shuffling string s and then add one more letter at a random position.Find the letter that was adde

haimianxiaodao的博客 710

ChromeOS安装教程

ChromeOS,是基于Linux内核研发的ChromiumOS开源系统的闭源系统,最大的特色就是内置了Chrome浏览器,早期主要以Web云端应用程序为主,后来逐渐引入了Android子系统和Linux子系统。本文主要简述摸索的家用笔记本安装ChromeOS安装方法,供各位玩机朋友参考。

weixin_45751495的博客 8182

LMK04828时钟芯片配置实战:从原理到调试的完整指南

锁相环(PLL)是时钟生成与分配的核心技术,通过相位反馈控制实现频率合成与抖动滤除。其原理基于鉴相器、环路滤波器和压控振荡器的协同工作,能够将低质量的参考时钟转换为高稳定、低抖动的输出信号,为高速数据转换、通信系统提供精准时序基础。在JESD204B/C等高速接口应用中,时钟的相位噪声和同步精度直接决定了系统性能上限。本文以TI高性能时钟芯片LMK04828为例,深入解析其双PLL架构与小数分频合成原理,并聚焦于SYSREF信号生成与同步配置这一关键实践,提供从频率规划、环路参数设计到SPI寄存器配置的全流

weixin_33747129的博客 483

【python】【leetcode】【算法题目389—Find the Difference】

一、题目描述 题目原文: Given two stringssandtwhich consist of only lowercase letters. Stringtis generated by random shuffling stringsand then add one more letter at a random position. Find

一只奋斗的小虫的博客 812

V4L2框架概述

几乎所有的设备都有多个 IC 模块,它们可能是实体的(例如 USB 摄像头里面包含 ISP、sensor 等)、也可能是抽象的(如 USB 设备里面的抽象拓扑结构),它们在/dev目录下面生成了多个设备节点,并且这些 IC 模块还创建了一些非 v4l2 设备:DVB、ALSA、FB、I2C 和输入设备。正是由于硬件的复杂性,v4l2 的驱动也变得非常复杂。特别是 v4l2 驱动要支持 IC 模块来进行音/视频的混合/编解码操作,这就更加使得 v4l2 驱动变得异常复杂。

YellowMax 8万+

【python3】leetcode 389. Find the Difference (easy)

 389. Find the Difference (easy) Given two strings s and t which consist of only lowercase letters. String t is generated by random shuffling string s and then add one more letter at a random posit...

momottyy的专栏 313

EPLAN 电气制图(六):结构盒与设备管理器核心概念(基础知识选看)

结构盒是 EPLAN 中用于标识元件安装位置的逻辑容器,非实际设备或黑盒,仅用于原理图中划分功能区域或物理位置(如 “柜内”“柜外”)。标识符设置:在 “显示设备标识符” 中输入位置代号(如 “+ 柜外”),完整格式为 “= 高层代号 + 位置代号”;筛选与定位:通过 “筛选器” 按类别(如电机、接触器)快速查找设备,右键 “转到(图形)” 可高亮定位图纸中的元件;设备保护:重要设备可启用保护(【项目数据】→【设备】→【启用设备保护】),防止误删除(显示橙色圆圈标记)。

电工 1067

【WRF-Urban数据集】中国60个城市的1 km分辨率城市冠层参数(UCPs)

中国60个城市的1 km分辨率城市冠层参数(UCPs)

WW、forever的博客 1080

LeetCode--Python解析【Find the Difference】(389)

题目:方法1:这道题的的解法类似于LeetCode--Python解析【Intersection of Two Arrays II】(350),第一种方法也用list的方法来解将s转为list,然后遍历t中的字符,若存在于s中,则删除s中的该字符若s中无该字符,则证明该字符是被添加的字符,返回该字符class Solution: def findTheDifference(self, s,...

ZJRN1027的博客 940

YOLO26涨点改进 | YOLO联合Mamba火热发论文改进篇 | CVPR 2025 | 引入EfficientViM 新型轻量级视觉Mamba,高效捕捉全局依赖,含二次创新、适合目标检测任务

本文提出EfficientViM,一种基于HSM-SSD的新型轻量级视觉Mamba架构,用于改进YOLO26目标检测模型。该模块通过压缩隐藏状态、多阶段特征融合和内存优化设计,在降低计算复杂度的同时提升检测精度。实验表明,EfficientViM在ImageNet-1K上实现了最佳的速度-精度平衡,相比SHViT模型性能提升0.7%且速度更快。文章详细介绍了模块原理、网络结构、核心代码实现及YOLO26集成方法,提供两种改进方案,适用于资源受限环境下的实时目标检测任务。

Ai缝合怪、助力小伙伴高效发论文 185

【LeetCode】389. Find the Difference 解题报告(Java & Python)

【LeetCode】Find the Difference 解题报告[LeetCode]https://leetcode.com/problems/find-the-difference/ Difficulty: Easy Question Given two strings s and t which consist of only lowercase letters. String

负雪明烛 1506

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

leetcode 389. Find the Difference 牛人用异或 或者 求和 解决,很简单。

389. Find the Difference   Question Editorial Solution  My Submissions Total Accepted: 7465Total Submissions: 14609Difficulty: Easy Given two strings s and t which consis

有志者事竟成 1254

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

[leetcode: Python]389. Find the Difference

题目: Given two strings s and t which consist of only lowercase letters.String t is generated by random shuffling string s and then add one more letter at a random position.Find the letter that was adde

cyy2learn的博客 847

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

python--leetcode389. Find the Difference

Given two strings s and t which consist of only lowercase letters. String t is generated by random shuffling string s and then add one more letter at a random position. Find the letter that was adde

wenqiwenqi123的博客 713

python leetcode 389. Find the Difference

class Solution: def findTheDifference(self, s, t): &quot;&quot;&quot; :type s: str :type t: str :rtype: str &quot;&quot;&quot; dp=[0]*26 for i in range(len(s)):

Neekity的博客 638

389. Find the Difference(python+cpp)

题目: Given two strings s and t which consist of only lowercase letters. String t is generated by random shuffling string s and then add one more letter at a random position. Find the letter that was a...

小湉湉的博客 233

1266. Find the Difference

描述Given two strings s and t which consist of only lowercase letters.String t is generated by random shuffling string s and then add one more letter at a random position.Find the letter that was added ...

vestlee的博客 190

Find the Difference问题及解法

问题描述: Given two strings s and t which consist of only lowercase letters. String t is generated by random shuffling string s and then add one more letter at a random position. Find the letter th

刘天的博客 701

给定两个字符串 s 和 t,它们只包含小写字母。 字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母。 请找出在 t 中被添加的字母

Title: 找不同 给定两个字符串 s 和 t,它们只包含小写字母。 字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母。 请找出在 t 中被添加的字母。 力扣原题传送 提示: 0 <= s.length <= 1000 t.length == s.length + 1 s 和 t 只包含小写字母 解题思路: 分析题目我们发现字符串只存在一个不同的字符,因此我们可以使用位运算(异或)的特点解题,代码如下: class Solution { public: char find

nepu_bin的博客 3141

Leetcode--Find the Difference[python]

题目解析: 有两个字符串s和t,s中随机插入一个字符得到t。求随机插入的字符。 例子: 输入: s = “abcd” t = “abcde”输出: e解法:(异或方法) class Solution(object): def findTheDifference(self, s, t): “”” :type s: str :

脸脸的专栏 438

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

给定两个字符串 s 和 t,它们只包含小写字母。 字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母。 请找出在 t 中被添加的字母。 示例: 输入: s = “abcd” t = “abcde” 输出: e 解释: ‘e’ 是那个被添加的字母。 class Solution { public: char findTheDifference(string s, string t) { int index1[128] = {0},index2[128] = {0};

weixin_43728206的博客 3735

给定两个字符串 s 和 t,它们只包含小写字母。 字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母。 请找出在 t 中被添加的字母。...

给定两个字符串 s 和 t,它们只包含小写字母。字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母。请找出在 t 中被添加的字母。 示例:   输入:     s = "abcd"     t = "abcde"   输出:     e 解题思路:该题的解法和上一篇我们解决问题的思路一样,同样此题我们需要定义两个数组arr1和arr2分别存储字符串s和...

diannuo5927的博客 5033

389. Find the Difference

Given two strings s and t which consist of only lowercase letters. String t is generated by random shuffling string s and then add one more letter at a random position. Find the letter that was added in t.

myfwjy的博客 442
上一篇: 347. Top K Frequent Elements [medium] (Python)
下一篇: 383. Ransom Note [easy] (Python)
coder_orz
博客等级 码龄14年 102粉丝 90原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址