Code Signal_练习题_commonCharacterCount
Given two strings, find the number of common characters between them.
Example
For s1 = "aabcc" and s2 = "adcaa", the output should becommonCharacterCount(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的更多相关文章
- Code Signal_练习题_digitDegree
Let's define digit degree of some positive integer as the number of times we need to replace this nu ...
- Code Signal_练习题_Knapsack Light
You found two items in a treasure chest! The first item weighs weight1 and is worth value1, and the ...
- Code Signal_练习题_growingPlant
Each day a plant is growing by upSpeed meters. Each night that plant's height decreases by downSpeed ...
- Code Signal_练习题_arrayMaxConsecutiveSum
Given array of integers, find the maximal possible sum of some of its k consecutive elements. Exampl ...
- Code Signal_练习题_differentSymbolsNaive
Given a string, find the number of different characters in it. Example For s = "cabca", th ...
- Code Signal_练习题_firstDigit
Find the leftmost digit that occurs in a given string. Example For inputString = "var_1__Int&qu ...
- Code Signal_练习题_extractEachKth
Given array of integers, remove each kth element from it. Example For inputArray = [1, 2, 3, 4, 5, 6 ...
- Code Signal_练习题_stringsRearrangement
Given an array of equal-length strings, check if it is possible to rearrange the strings in such a w ...
- 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) + ...
随机推荐
- 使用putty连接本地VirtualBox上的centos7 linux主机
1. 查看linux主机默认ssh端口 因为是使用ssh连接虚拟机上的linux主机的,所以需要查看centos ssh默认端口,一般是22 打开终端 输入cd /etc/ssh/ 查看ssh_con ...
- 进程控制块(PCB)
用来描述和控制进程的运行的一个数据结构--进程控制块PCB(Process Control Block),是进程实体的一部分,是操作系统中最重要的记录型数据结构. PCB是进程存在的唯一标志 系统能且 ...
- angular.js的依赖注入解析
本教程使用AngularJS版本:1.5.3 angularjs GitHub: https://github.com/angular/angular.js/ Angula ...
- template.js插件和ajax一起使用的例子
template.js 一款 JavaScript 模板引擎,简单,好用.提供一套模板语法,用户可以写一个模板区块,每次根据传入的数据,生成对应数据产生的HTML片段,渲染不同的效果. https:/ ...
- OS之内存管理 ---基本的内存管理策略(二)
分段 基本方法 分段就是基于用户视图的内存管理方案.逻辑地址空间是由一组段构成的,每个段都有名称和长度.地址指定了段名称和段内偏移.因此用户通过两个量来指定地址:段名称和段偏移. 为了简单,进行对段的 ...
- Linux Cluster环境下批量分发执行补丁
转自:http://blog.csdn.net/napolunyishi/article/details/18219867 这两天做了一个需求,因为上一个版本的/tmp空间默认只分配了5G,而升级程序 ...
- java android中日期时间 问题总结
Date 类型: Date date = new Date(); // 代表获取当前系统日期和时间 System.out.println(date); 使用类的方法设置时间和日期:(通过该方法初始 ...
- JAVA 垃圾笔记一溜堆
精度只能从低精度 转到高精度.例如:float = 3.4;错误 默认小数在JAVA中是double. 即:从double高精度转到floag低精度错误!!将字符char加减乘除,默认对ASCII码运 ...
- mysql笔记--group by,limit用法
table: id tag status a b c d 一.group by用法 .与count 联合计数 select status,count(*) from table group by st ...
- java aop做一个接口耗时的计算
看代码: @Aspect @Component public class TimeCostAspect { private static Logger logger = LoggerFactory.g ...