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

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

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

Post 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).
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply