Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@nikoheikkila
Created June 8, 2019 11:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nikoheikkila/8be688b6d8db732ecb2d4ac2217e8f80 to your computer and use it in GitHub Desktop.
Save nikoheikkila/8be688b6d8db732ecb2d4ac2217e8f80 to your computer and use it in GitHub Desktop.
Write a function that will return the count of distinct case-insensitive alphabetic characters and numeric digits that occur more than once in the input string.
def duplicate_count(text: str) -> int:
"""Write a function that will return the count of distinct case-insensitive
alphabetic characters and numeric digits that occur more than once in the
input string.
The input string can be assumed to contain only alphabets
(both uppercase and lowercase) and numeric digits.
"abcde" -> 0 # no characters repeat more than once
"aabbcde" -> 2 # 'a' and 'b'
"aabBcde" -> 2 # 'a' occurs twice and 'b' twice (`b` and `B`)
"indivisibility" -> 1 # 'i' occurs six times
"Indivisibilities" -> 2 # 'i' occurs seven times and 's' occurs twice
"aA11" -> 2 # 'a' and '1'
"ABBA" -> 2 # 'A' and 'B' each occur twice
"""
if not text: return 0
text = text.lower()
return len(set([char for char in text if text.count(char) > 1]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment