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. Divide and Conquer-169. Majority Element

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  2. window7下Python安装

    去python官网下载软件,https://www.python.org. 选择dowloads-->windows,选择稳定版python3.5.2,x86表示32位操作系统,x86-64表示 ...

  3. python-xlwt给excel添加样式

    #coding:utf-8import osimport time        import xlwt filename="test_xlwt.xls"if os.path.ex ...

  4. 字蛛fontSpider的使用

    字蛛官方文档 http://font-spider.org/index.html 首先安装全局包  npm install font-spider -g 然后下载字体 ,本次需要的是  "造 ...

  5. 【OpenCV3】cvRound()、cvFloor()、cvCeil()函数详解

    函数cvRound().cvFloor().cvCeil()都是按照一种舍入方式将浮点型数据转换为整型数据. cvRound():返回跟参数最接近的整数值,即四舍五入: cvFloor()  :返回不 ...

  6. 【转】linux下杀死进程(kill)的N种方法

    转载一篇,最原始的出处已不可考,望见谅! 常规篇: 首先,用ps查看进程,方法如下: $ ps -ef ……smx       1822     1  0 11:38 ?        00:00:4 ...

  7. 第5章—构建Spring Web应用程序—SpringMVC详解

    SpringMVC详解 5.1.跟踪Springmvc的请求 SpringMVC的核心流程如下: 具体步骤: 第一步:发起请求到前端控制器(DispatcherServlet) 第二步:前端控制器请求 ...

  8. Python虚拟环境中pip install时没有权限问题

    virtualenv Permission denied 新建的python虚拟环境的目录的所属者必须是当前用户,才不会出现这种错误 比如 virtualenv py27 sudo chown zzf ...

  9. 安装freepbx后创建sip分机

    在前面的文章阿里云使用镜像安装freepbx里面我们已经使用镜像文件安装好了freepbx,接下来开始是开始创建SIP分机,实现可以拨打电话. 首先我们输入我们的IP可以直接访问到freepbx的界面 ...

  10. Mahout实战---评估推荐程序

    推荐程序的一般评测标准有MAE(平均绝对误差),Precision(查准率),recall(查全率) 针对Mahout实战---运行第一个推荐引擎 的推荐程序,将使用上面三个标准分别测量 MAE(平均 ...