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

leetcode - 389. Find the Difference

Description

You are given two strings s and t.

String t is generated by random shuffling string s and then add one more letter at a random position.

Return the letter that was added to t.

Example 1:

Input: s = “abcd”, t = “abcde”
Output: “e”
Explanation: ‘e’ is the letter that was added.
Example 2:

Input: s = “”, t = “y”
Output: “y”

Constraints:

0 <= s.length <= 1000
t.length == s.length + 1
s and t consist of lowercase English letters.

Solution

Dict

Use a dict to compare s and t

Bit Manipulation

If the character appears both in s and t, then it must appear multipliers of 2. So we could use xor.

Time complexity: o ( m + n ) o(m+n) o(m+n). where m and n are lengths of s and t
Space complexity: o ( 1 ) o(1) o(1)

Code

Dict

class Solution:def findTheDifference(self, s: str, t: str) -> str:chars = {}for ch in s:chars[ch] = chars.get(ch, 0) + 1for ch in t:if ch not in chars:return chchars[ch] -= 1if chars[ch] == 0:chars.pop(ch)return list(chars.keys())[0]

Bit Manipulation

class Solution:def findTheDifference(self, s: str, t: str) -> str:res = 0for ch in s:res ^= ord(ch)for ch in t:res ^= ord(ch)return chr(res)
http://www.lryc.cn/news/175563.html

相关文章:

  • asp.net企业生产管理系统VS开发sqlserver数据库web结构c#编程Microsoft Visual Studio
  • vue中或者react中的excel导入和导出
  • 如何在Ubuntu的根目录下创建删除目录
  • lv5 嵌入式开发-6 线程的取消和互斥
  • el-table实现穿梭功能
  • Cron表达式_用于定时调度任务
  • CeresPCL ICP精配准(点到面)
  • CentOS安装kafka单机部署
  • 蓝牙核心规范(V5.4)11.2-LE Audio 笔记之LE Auido架构
  • 福建江夏学院蔡慧梅主任一行莅临拓世科技集团,共探AI+时代教育新未来
  • 使用Visual Leak Detector排查内存泄漏问题
  • 如何设计一个 JVM 语言下的 LLM 应用开发框架?以 Chocolate Factory 为例
  • 基础排序算法
  • Nginx的反向代理、动静分离、负载均衡
  • LLM-TAP随笔——大语言模型基础【深度学习】【PyTorch】【LLM】
  • 蓝桥杯备赛-上学迟到
  • 基于 MATLAB 的电力系统动态分析研究【IEEE9、IEEE68系节点】
  • 2023百度之星 题目详解 公园+糖果促销
  • C++ 2019-2022 CSP_J 复赛试题横向维度分析(中)
  • 基于Spring Boot的IT技术交流和分享平台的设计与实现
  • 智算引领·创新未来 | 2023紫光展锐泛物联网终端生态论坛成功举办
  • 网络安全技术指南 103.91.209.X
  • 用flex实现grid布局
  • 东郊到家app小程序公众号软件开发预约同城服务系统成品源码部署
  • kotlin的集合使用maxBy函数报NoSuchElementException
  • Python开发与应用实验2 | Python基础语法应用
  • 网络安全--防火墙旁挂部署方式和高可靠性技术
  • c++最小步数模型(魔板)
  • 【每日一题Day337】LC460LFU 缓存 | 双链表+哈希表
  • 解决老版本Oracle VirtualBox 此应用无法在此设备上运行问题