题目要求

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.

题目分析及思路

给定两个只由小写字母组成的字符串s和t,其中t是在s字符串组成元素的基础上再添加一个字母构成的。注意这里添加的字母可能和已有的字母重复。要求找到新添加的字母。可以使用collections.Counter统计每个字符串中不同字母出现的次数,然后遍历字典t的key,只要这个key不在字典s中或者该key对应的value不相等,则这个key就是我们要找的字母。

python代码

class Solution:

def findTheDifference(self, s: str, t: str) -> str:

s = collections.Counter(s)

t = collections.Counter(t)

for k in t:

if k not in s or t[k] != s[k]:

return k

LeetCode 389 Find the Difference 解题报告的更多相关文章

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

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:字典统计次数 方法二:异或 方法三:排序 日 ...

  2. 【LeetCode】376. Wiggle Subsequence 解题报告(Python)

    [LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...

  3. LeetCode 2 Add Two Sum 解题报告

    LeetCode 2 Add Two Sum 解题报告 LeetCode第二题 Add Two Sum 首先我们看题目要求: You are given two linked lists repres ...

  4. 【LeetCode】649. Dota2 Senate 解题报告(Python)

    [LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

  5. 【LeetCode】911. Online Election 解题报告(Python)

    [LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...

  6. 【LeetCode】886. Possible Bipartition 解题报告(Python)

    [LeetCode]886. Possible Bipartition 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...

  7. 【LeetCode】36. Valid Sudoku 解题报告(Python)

    [LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...

  8. 【LeetCode】870. Advantage Shuffle 解题报告(Python)

    [LeetCode]870. Advantage Shuffle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...

  9. 【LeetCode】593. Valid Square 解题报告(Python)

    [LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

随机推荐

  1. js cookie跨域设置

    /** * 设置cookie方法 * @param {string} c_name cookie键值 * @param {string} value cookie值 * @param {Boolean ...

  2. 【神经网络】Dependency Parsing的两种解决方案

    一.Transition-based的依存解析方法 解析过程:首先设计一系列action, 其就是有方向带类型的边,接着从左向右依次解析句子中的每一个词,解析词的同时通过选择某一个action开始增量 ...

  3. bash python获取文本中每个字符出现的次数

    bash: grep -o . myfile | sort |uniq -c python:  使用collections模块 import pprint import collections f = ...

  4. redhat杂记

    1.设置sudo权限:修改/etc/sudoers文件,找到root    ALL=(ALL)       ALL,在后面添加nginx ALL=(ALL) NOPASSWD: ALL 2.用sed命 ...

  5. Ubuntu 10.04下架设流媒体服务器

    Ubuntu 10.04下架设流媒体服务器 个人建议:使用DarwinStreamingSrvr5.5.5,因为DarwinStreamingSrvr6.0.3安装过程中有很多问题需要解决! 目前主流 ...

  6. Inside The C++ Object Model(三)

    ============================================================================3-0. 类所占的内存大小受到三个因素的影响:( ...

  7. python把文件从一个目录复制到另外一个目录,并且备份

    #!/usr/bin/python # -*- coding: utf-8 -*- import os,sys,md5,datetime,shutil,time,zipfile,chardet # c ...

  8. 【Java核心技术】类型信息(Class对象 反射 动态代理)

    1 Class对象 理解RTTI在Java中的工作原理,首先需要知道类型信息在运行时是如何表示的,这是由Class对象来完成的,它包含了与类有关的信息.Class对象就是用来创建所有“常规”对象的,J ...

  9. CentOS7安装Go环境

    下载go(我的当前目录是/data/work)$wget https://studygolang.com/dl/golang/go1.10.1.linux-amd64.tar.gz$tar -xvf ...

  10. share drive 无效

    docker设置的share dirve怎么按都无效 试了几遍都不行,想想刚才电脑系统更新了,然后查了下百度,发现是电脑策略的问题,设置成经典的就可以了