Given two strings, find the number of common characters between them.

Example

For s1 = "aabcc" and s2 = "adcaa", the output should be
commonCharacterCount(s1, s2) = 3.

Strings have 3 common characters - 2 "a"s and 1 "c".

我的解答:

 def commonCharacterCount(s1, s2):
sum = 0
for i in set(s1):
m = min(s1.count(i),s2.count(i))
sum += m
return sum

想了半天才想出来用set,知识都知道,但就是想不起来用,还是练得少啊

膜拜大佬:

一位美国大佬写的(排名靠前的基本都是这么写...):
def commonCharacterCount(s1, s2):
return sum(min(s1.count(x), s2.count(x)) for x in set(s1))

Code Signal_练习题_commonCharacterCount的更多相关文章

  1. Code Signal_练习题_digitDegree

    Let's define digit degree of some positive integer as the number of times we need to replace this nu ...

  2. Code Signal_练习题_Knapsack Light

    You found two items in a treasure chest! The first item weighs weight1 and is worth value1, and the ...

  3. Code Signal_练习题_growingPlant

    Each day a plant is growing by upSpeed meters. Each night that plant's height decreases by downSpeed ...

  4. Code Signal_练习题_arrayMaxConsecutiveSum

    Given array of integers, find the maximal possible sum of some of its k consecutive elements. Exampl ...

  5. Code Signal_练习题_differentSymbolsNaive

    Given a string, find the number of different characters in it. Example For s = "cabca", th ...

  6. Code Signal_练习题_firstDigit

    Find the leftmost digit that occurs in a given string. Example For inputString = "var_1__Int&qu ...

  7. Code Signal_练习题_extractEachKth

    Given array of integers, remove each kth element from it. Example For inputArray = [1, 2, 3, 4, 5, 6 ...

  8. Code Signal_练习题_stringsRearrangement

    Given an array of equal-length strings, check if it is possible to rearrange the strings in such a w ...

  9. Code Signal_练习题_absoluteValuesSumMinimization

    Given a sorted array of integers a, find an integer x from a such that the value of abs(a[0] - x) + ...

随机推荐

  1. netstat 查看本机开放端口

    root@kali:~# netstat -luntp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Add ...

  2. jQuery基础笔记 each和data(7)

    day56 参考:https://www.cnblogs.com/liwenzhou/p/8178806.html#autoid-1-11-0 each jQuery.each(collection, ...

  3. nodejs实现请求代理

    通常我们常用的请求方法只有GET.POST.PUT和DELETE,所以在此只介绍这四种和文件上传的代理方式 在此我们使用request.js第三方模块实现 GET(DELETE同GET,将reques ...

  4. 在 iOS 上编译 webkit 源码

    准备工作 买一台 mac 下载并安装 Xcode 下载源码 git clone git://git.webkit.org/WebKit.git WebKit 这个可能要耗费很久很久 编译源码 打开 X ...

  5. 企业IM (或业务系统)web api的json格式设计思考(原创)

    在企业IM开发中,经常用到和业务系统的数据交换,在中国企业最常见的比如组织架构变更,一般在客户端加密保存了组织架构树(便于快速的查询和树展示),当HR或OA或AD域这些管控企业组织架构的数据发生改变, ...

  6. C#获取获取北京时间多种方法

    #region 获取网络时间 ///<summary> /// 获取中国国家授时中心网络服务器时间发布的当前时间 ///</summary> ///<returns> ...

  7. Centos7下安装zabbix 3.0.19

    参考网站: https://www.cnblogs.com/xiewenming/p/7732144.html https://www.cnblogs.com/clsn/p/7885990.html  ...

  8. 11、使用xamarin实现全屏播放rtmp之类的直播视频

    直播类的app大部分都是以rtmp hls播放为主.目前主流的app解决方案大部分是引入ijkplayer 这个是基于ffmpeg中的ffplayer实现的. 众所周知ffmpeg的解码能力是一流的. ...

  9. (转)linux如何让历史记录不记录敏感命令

    有时候为了服务器安全,防止别人窥探我们输入的命令,我们可以清空历史记录,而更多的时候,我们选择的是在输入特殊命令时候,强制历史记录不记住该命令.实验方法:先执行export HISTCONTROL=i ...

  10. springBoot上传文件时MultipartFile报空问题解决方法

    springBoot上传文件时MultipartFile报空问题解决方法 1.问题描述: 之前用spring MVC,转成spring boot之后发现上传不能用.网上参考说是spring boot已 ...