Problem:
Score: 400 points

Problem Statement

AtCoder Inc. has decided to lock the door of its office with a 3-digit PIN code.

The company has an N-digit lucky number, S. Takahashi, the president, will erase N-3 digits from S and concatenate the remaining 3 digits without changing the order to set the PIN code.

How many different PIN codes can he set this way?

Both the lucky number and the PIN code may begin with a 0.

Constraints

4 \leq N \leq 30000

S is a string of length N consisting of digits.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the number of different PIN codes Takahashi can set.

Sample Input 1

4
0224

Sample Output 1

3

Takahashi has the following options:

Erase the first digit of S and set 224.

Erase the second digit of S and set 024.

Erase the third digit of S and set 024.

Erase the fourth digit of S and set 022.

Thus, he can set three different PIN codes: 022, 024, and 224.

Sample Input 2

6
123123

Sample Output 2

17

Sample Input 3

19
3141592653589793238

Sample Output 3

329

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

use std::collections::HashSet;
use std::io::*;
use std::str::*;

fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let s = stdin
        .bytes()
        .map(|c| c.unwrap() as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect::<String>();
    s.parse::<T>().ok().unwrap()
}

fn string() -> String { read() }

fn usize() -> usize {
    read()
}

fn main() {
    let mut results: HashSet<usize> = HashSet::new();

    let n = usize();
    let s: Vec<u8> = string().chars().map(|c| {
        c.to_digit(10).unwrap() as u8
    }).collect();

    let mut s1_set: HashSet<usize> = HashSet::new();
    let mut s12_set: HashSet<usize> = HashSet::new();
    for s1i in 0..n {
        let s1 = s[s1i] as usize;
        if s1_set.contains(&s1) {
            continue;
        }
        s1_set.insert(s1);
        for s2i in s1i + 1..n {
            let s2 = s[s2i] as usize;
            let s12 = s1 * 10 + s2;
            if s12_set.contains(&s12) {
                continue;
            }
            s12_set.insert(s12);
            for s3i in s2i + 1..n {
                let s3 = s[s3i] as usize;
                results.insert(s1 * 100 + s2 * 10 + s3);
            }
        }
    }
    println!("{}", results.len());
}