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. Java直接内存与非直接内存性能测试

    什么是直接内存与非直接内存 根据官方文档的描述: A byte buffer is either direct or non-direct. Given a direct byte buffer, t ...

  2. jade直接写类似JavaScript语法的东西,不需要写script

    我们知道,html做计算都是在JavaScript中完成的,那么不用JavaScript行不行呢,可以直接在jade中一样的编写 如: -var a = 3 -var b = 4 div a+b = ...

  3. Scrapy源码注解--CookiesMiddleware

    class CookiesMiddleware(object): """ 中间件在Scrapy启动时实例化.其中jars属性是一个默认值为CookieJar对象的dict ...

  4. 利用BitviseSSH免root实现Windows vs Linux的文件互传

    虚拟机截图,,,质量有点差,大家看看! ------------------- 在拿不到Linux root账户的情况下,winscp等工具是无法实现文件传输的,此时我们可以借用Bitvise SSH ...

  5. Java之集合(十八)DelayQueue

    转载请注明源出处:http://www.cnblogs.com/lighten/p/7493735.html 1.前言 本章介绍阻塞队列DelayQueue,这是一个无界阻塞队列.其存储延时的元素,只 ...

  6. 快速初步了解Neo4j与使用

    快速初步了解Neo4j与使用 Neo4j是一个高性能的,NOSQL图形数据库,它将结构化数据存储在网络上而不是表中.它是一个嵌入式的.基于磁盘的.具备完全的事务特性的Java持久化引擎,但是它将结构化 ...

  7. 暴力攻击 PHP 脚本 初探

    考虑下面的HTML表单: CODE: <form action="http://example.org/login.php" method="POST"& ...

  8. Android 开发工具类 05_Logcat 统一管理类

    Logcat 统一管理类: 1.默 认tag 的函数: 2.自定义 tag 的函数. import android.util.Log; // Logcat 统一管理类 public class L { ...

  9. Elasticsearch入门(一)

    索引(index) -- 存储关联数据的地方.实际上,索引只是一个逻辑命名空间(logical namespace),它指向一个或多个分片(shards). 分片(shard) 是 工作单元(work ...

  10. redis报Cannot allocate memory错误

    昨天16:27开始将dp的日志使用ELK处理,当时redis使用内存的量不是很大,100M多点,结果今天早上到了一看xshell被关掉了,赶紧将各服务启动起来,elasticsearch启动没有问题, ...