코드로 우주평화

Counting Duplicates 본문

나는 이렇게 학습한다/Algorithm & SQL

Counting Duplicates

daco2020 2022. 3. 1. 21:46
반응형

Description:

Count the number of Duplicates

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.

Example

"abcde" -> 0 # no characters repeats 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

 

Solution:

1. Make a string lowercase.
2. Count the number of each character in the array.
3. Count and return the number of characters with two or more identical characters in the array.

 

def duplicate_count(text):
    text = text.lower()
    count = len([text.count(i) for i in set(text) if text.count(i)>1])
    return count

 

 

 

반응형

'나는 이렇게 학습한다 > Algorithm & SQL' 카테고리의 다른 글

Sum of Numbers  (0) 2022.03.03
Grasshopper - Summation  (0) 2022.03.03
Growth of a Population  (0) 2022.03.01
Binary Addition  (0) 2022.02.27
Unique In Order  (0) 2022.02.26