Skip to content

Instantly share code, notes, and snippets.

@tyler-austin
Created May 31, 2017 22:02
Show Gist options
  • Save tyler-austin/ae3dae6046e3b019bf0ec61b74f065e3 to your computer and use it in GitHub Desktop.
Save tyler-austin/ae3dae6046e3b019bf0ec61b74f065e3 to your computer and use it in GitHub Desktop.
Code Fights - commonCharacterCount

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

Input/Output

  • [time limit] 4000ms (py3)

  • [input] string s1

    • A string consisting of lowercase latin letters a-z.

Guaranteed constraints:

1 ≤ s1.length ≤ 15.
  • [input] string s2

    *A string consisting of lowercase latin letters a-z.

Guaranteed constraints:

1 ≤ s2.length ≤ 15.
  • [output] integer
def common_character_count(str1: str, str2: str):
str1_set = set(str1)
result = 0
for letter in str1_set:
str1_count = str1.count(letter)
str2_count = str2.count(letter)
result += min([str1_count, str2_count])
return result
if __name__ == '__main__':
s1 = "aabcc"
s2 = "adcaa"
print(common_character_count(s1, s2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment