Duplicate Character Counter

Easy
0/40
profile
Contributed by
0 upvote

Problem statement

You are given a string S containing only lowercase English alphabets. Your task is to count the number of distinct characters that appear more than once in the string.


If no character repeats (i.e., every character appears exactly once), you should print -1. Otherwise, print the count of distinct characters that have duplicates.


Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first and only line of input contains a single string S.


Output Format:
If no character repeats, output a single integer -1.
Otherwise, output a single integer representing the count of distinct characters that appear more than once.


Note:
The count is of the characters themselves, not the total number of times they appear. For example, in the string "banana", the character 'a' repeats and the character 'n' repeats. The count of distinct repeating characters is 2.

The comparison is case-sensitive, but the input is guaranteed to be all lowercase alphabets.
Sample Input 1:
programming


Sample Output 1:
3


Explanation for Sample 1:
In the string "programming":
- 'r' appears 2 times.
- 'g' appears 2 times.
- 'm' appears 2 times.
- The other characters ('p', 'o', 'a', 'i', 'n') appear only once. The distinct characters that repeat are 'r', 'g', and 'm'. There are 3 such characters.


Expected Time Complexity:
The expected time complexity for a single query is O(1) after an initial O(R * C) pre-computation. The total complexity is O(R * C).


Sample Input 2:
unique


Sample Output 2:
-1


Explanation for Sample 2:
The expected time complexity is O(N), where N is the length of the string S.


Constraints:
1 <= S.length <= 10^5

Time limit: 1 sec
Approaches (1)
Frequency Counting with a Hash Map or Array
Time Complexity
Space Complexity
Code Solution
(100% EXP penalty)
Duplicate Character Counter
Full screen
Console