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<String> = HashSet::new();

    let n = usize();
    let s: Vec<char> = string().chars().collect();

    let mut s1_set: HashSet<String> = HashSet::new();
    let mut s12_set: HashSet<String> = HashSet::new();
    for s1i in 0..n {
        let mut buf1: Vec<char> = Vec::new();
        buf1.push(s[s1i]);
        let s1: String = buf1.clone().into_iter().collect();
        if s1_set.contains(&s1) {
            continue;
        }
        s1_set.insert(s1);
        for s2i in s1i + 1..n {
            let mut buf2 = buf1.clone();
            buf2.push(s[s2i]);
            let s12: String = buf2.clone().into_iter().collect();
            if s12_set.contains(&s12) {
                continue;
            }
            s12_set.insert(s12);
            for s3i in s2i + 1..n {
                let mut buf3 = buf2.clone();
                buf3.push(s[s3i]);
                results.insert(buf3.into_iter().collect());
            }
        }
    }
    println!("{}", results.len());
}