Q. 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
S. 먼저 텐스트를 모두 소문자로 바꾸고, set으로 바꾼 다음 각 set 요소가 문자열에 두 개 이상 있으면 result에 +1을 해주었다.
int duplicateCount(String text) {
int result = 0;
final str = text.toLowerCase();
str
.split('')
.toSet()
.forEach((e) => result += e.allMatches(str).length >= 2 ? 1 : 0);
return result;
}
'알고리즘 > Codewars' 카테고리의 다른 글
[Codewars] Tribonacci Sequence (0) | 2024.06.11 |
---|---|
[Codewars] Exes and Ohs (0) | 2024.06.10 |
[Codewars] Create Phone Number (0) | 2024.06.09 |
[Codewars] Find The Parity Outlier (1) | 2024.06.09 |
[Codewars] You're a square! (0) | 2024.06.09 |