This page is still under construction.

Parts of this page are still being built. What you see may change.

Pekgalsvalsen

Interview

Time limit2sMemory limit1024 MB

Summary
Given a string with K distinct letters, choose the order to write the letters so that the total right-arrow distance is minimized, and print that minimum.
Level

Medium6 of 10

Topics
Greedy, Sorting, Math, String
Solved
No attempts yet

Problem

Doris is writing a long email to her family. The text consists of KK different letters and is NN letters long. Doris has a poor memory, so she cannot remember where the letters are on the keyboard. Instead she uses a variant of the so-called pekgalsvalsen when she types.

Doris types her text in KK rounds, one round for each of the different letters that make up the text. First Doris writes down all occurrences of one of the letters with one of her gills. Then she goes back to the beginning of the text and picks a new letter. She then writes all occurrences of this letter, using the right arrow key with her other gill. After that she goes back to the beginning of the text again, now to write down the third letter, and so on. She repeats this until she has written the whole text. In this way she only needs to remember where the key for a single letter is at a time.

Depending on the order in which she chooses to write the letters, this can take different amounts of time. If she is to write the text aabbac, the order a, b, c requires her to press the right arrow key 7 times. First she writes aaa without using the right arrow. Then she goes back to the text, uses the right arrow twice and writes bb. Finally she goes back to the beginning, goes right five times and writes c.

If she instead chose the order b, a, c, she would first have written bb. Then she would have used two right presses to write aabba, and finally five more times to write aabbac.

The order c, b, a requires her to press the right arrow only twice, which is optimal.

Input

The first line contains the positive integers NN and KK. The next line contains a string of NN characters, chosen among the first KK lowercase letters of the alphabet (a, b, c, ...).

Output

Print a single number: the number of times Doris must press the right arrow to write the text.

Examples2

  1. Example 1

    Input
    6 3
    aabbac
    
    Expected output
    2
    
  2. Example 2

    Input
    10 2
    aaabaaabbb
    
    Expected output
    1