Page 1 of 1

Write the code in Java only. Write a Function: class Solution { public int solution(String S); } that, given a string

Posted: Fri May 20, 2022 10:04 am
by answerhappygod
Write the code in Java only.
Write a Function:
class Solution { public int solution(String S);
}
that, given a string S consisting N lowercase letters, returns
the minimum of letters that must be deleted to obtaining a word in
which every letter occurs a unique number of times. We only care
about the occurrences of letters that appear at least once in a
result.
Examples:
1.Given S= "aaaabbbb", the function should return 1. We can
delete one occurrence of a or one occurrence of b. Then one letter
will occur four times and the other one three times.
2. Given S= "ccaaffddecee", the function should return 6. For
example, we can delete all occurrence of d to obtain the word
"ccaadc". Note that both e and f will occur zero times in a new
word, but that is fine, since we only care about letters that
appear at least once.
3. Given S="eeee", the function should return 0(there is no need
to delete any characters).
4. Given S="example", the function should return 4.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [0..300,000];
-string S consists only of lowercase letters (a-z).