Problem:
Score : 200 points

Problem Statement

You are given a string S of length N consisting of lowercase English letters.
We will cut this string at one position into two strings X and Y.
Here, we would like to maximize the number of different letters contained in both X and Y.
Find the largest possible number of different letters contained in both X and Y when we cut the string at the optimal position.

Constraints

2 \leq N \leq 100

|S| = N

S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the largest possible number of different letters contained in both X and Y.

Sample Input 1

6
aabbca

Sample Output 1

2

If we cut the string between the third and fourth letters into X = aab and Y = bca, the letters contained in both X and Y are a and b.
There will never be three or more different letters contained in both X and Y, so the answer is 2.

Sample Input 2

10
aaaaaaaaaa

Sample Output 2

1

However we divide S, only a will be contained in both X and Y.

Sample Input 3

45
tgxgdqkyjzhyputjjtllptdfxocrylqfqjynmfbfucbir

Sample Output 3

9

Solution in Rust (1.15.1) (judge verdict: Accepted)

use std::cmp::max;
use std::collections::HashMap;
use std::collections::HashSet;
use std::io::prelude::*;
fn main() {
    let stdin = std::io::stdin();
    let s: String = stdin
        .lock()
        .lines()
        .skip(1)
        .next()
        .expect("not two lines")
        .expect("read error");
    println!("{}", solve(&s.trim()));
}

fn solve(s: &str) -> u32 {
    let mut right: HashMap<char, u32> = HashMap::new();
    let mut left: HashSet<char> = HashSet::new();
    for c in s.chars() {
        let count = right.entry(c).or_insert(0);
        *count += 1;
    }
    let mut commons_max = 0;
    let mut commons = 0;
    for c in s.chars() {
        let right_count = right.get_mut(&c).unwrap();
        *right_count -= 1;
        if left.insert(c) {
            if *right_count > 0 {
                commons += 1;
                commons_max = max(commons, commons_max);
            }
        } else {
            if *right_count == 0 {
                commons -= 1;
            }
        }
    }
    commons_max
}