codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

672B.md (1804B)


      1 Source: https://codeforces.com/problemset/problem/672/B
      2 
      3 B. Different is Good
      4 
      5 time limit per test
      6 
      7 2 seconds
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 A wise man told Kerem "Different is good" once, so Kerem wants all things in his life to be different.
     22 
     23 Kerem recently got a string *s* consisting of lowercase English letters. Since Kerem likes it when things are different, he wants all substrings of his string *s* to be distinct. Substring is a string formed by some number of consecutive characters of the string. For example, string "aba" has substrings "" (empty substring), "a", "b", "a", "ab", "ba", "aba".
     24 
     25 If string *s* has at least two equal substrings then Kerem will change characters at some positions to some other lowercase English letters. Changing characters is a very tiring job, so Kerem want to perform as few changes as possible.
     26 
     27 Your task is to find the minimum number of changes needed to make all the substrings of the given string distinct, or determine that it is impossible.
     28 
     29 Input
     30 
     31 The first line of the input contains an integer *n* (1 ≤ *n* ≤ 100 000) — the length of the string *s*.
     32 
     33 The second line contains the string *s* of length *n* consisting of only lowercase English letters.
     34 
     35 Output
     36 
     37 If it's impossible to change the string *s* such that all its substring are distinct print -1. Otherwise print the minimum required number of changes.
     38 
     39 Examples
     40 
     41 Input
     42 
     43 ```
     44 2  
     45 aa
     46 ```
     47 
     48 Output
     49 
     50 ```
     51 1
     52 ```
     53 
     54 Input
     55 
     56 ```
     57 4  
     58 koko
     59 ```
     60 
     61 Output
     62 
     63 ```
     64 2
     65 ```
     66 
     67 Input
     68 
     69 ```
     70 5  
     71 murat
     72 ```
     73 
     74 Output
     75 
     76 ```
     77 0
     78 ```
     79 
     80 Note
     81 
     82 In the first sample one of the possible solutions is to change the first character to 'b'.
     83 
     84 In the second sample, one may change the first character to 'a' and second character to 'b', so the string becomes "abko".