TopcoderArchiveVisit Topcoder
SRM2 Mar 2016
SRM 684

Istr

Problem statement, definition, constraints, and public examples.

Problem Statement

Hero came up with an interesting way to calculate the value of any string. It works as follows:
  1. Find all distinct characters that appear in the string.
  2. For each of those characters, count the number of occurrences.
  3. Square each of those counts.
  4. Sum all those squares to get the value of the string.

For example, suppose Hero has the string "abacaba". This string contains 4 'a's, 2 'b's, and 1 'c'. Thus, its value is 4*4 + 2*2 + 1*1 = 21.

You are given a String s and an int k. You are allowed to remove at most k characters from s. Your goal is to produce a string with the smallest possible value. Compute and return that value.

Definition

Class:
Istr
Method:
count
Parameters:
String, int
Returns:
int
Method signature:
int count(String s, int k)
(be sure your method is public)

Constraints

  • s will contain between 1 and 50 characters, inclusive.
  • Each character in s will be a lowercase letter ('a'-'z').
  • k will be between 0 and the length of s, inclusive.

Examples

  1. "aba"
    1
    Returns: 2
    The optimal strategy is to erase one of the two 'a's. This produces a string with value 1*1 + 1*1 = 2.
  2. "abacaba"
    0
    Returns: 21
  3. "abacaba"
    1
    Returns: 14
  4. "abacaba"
    3
    Returns: 6
  5. "abc"
    3
    Returns: 0
  6. "wersrsresesrsesrawsdsw"
    11
    Returns: 23
Back to all problems