Code Signal_缁冧範棰榑commonCharacterCount

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Code Signal_缁冧範棰榑commonCharacterCount相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/mon' title='mon'>mon   +=   cli   color   between   bsp   ret   def   set   

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".

 

鎴戠殑瑙g瓟锛?/strong>

1 def commonCharacterCount(s1, s2):
2     sum = 0
3     for i in set(s1):
4         m = min(s1.count(i),s2.count(i))
5         sum += m
6     return sum

鎯充簡鍗婂ぉ鎵嶆兂鍑烘潵鐢╯et锛岀煡璇嗛兘鐭ラ亾锛屼絾灏辨槸鎯充笉璧锋潵鐢紝杩樻槸缁冨緱灏戝晩

 

鑶滄嫓澶т浆锛?/span>

鎶€鏈垎浜浘鐗? id=
涓€浣嶇編鍥藉ぇ浣啓鐨?鎺掑悕闈犲墠鐨勫熀鏈兘鏄繖涔堝啓...)锛?
def commonCharacterCount(s1, s2):
    return sum(min(s1.count(x), s2.count(x)) for x in set(s1))
View Code

 

以上是关于Code Signal_缁冧範棰榑commonCharacterCount的主要内容,如果未能解决你的问题,请参考以下文章

6銆?0閬揓AVA鍩虹缂栫▼缁冧範棰樿窡绛旀

17-5-缁冧範

linux缁冧範

R|Shiny缁冧範

Java瀛︿範-娉涘瀷缁煎悎缁冧範

鍓嶇涔婮avaScript缁冧範绛夌浉鍏冲唴瀹?58