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)

// This code is generated by [cargo-atcoder](https://github.com/tanakh/cargo-atcoder)
// Original source code:
/*
#![allow(unused_imports)]
use itertools::Itertools;
use proconio::input;
use proconio::marker::Chars;
use std::cmp::*;
use std::collections::*;
fn main() {
    input! {
        n: usize,
        s: Chars,
    }
    let s: Vec<_> = s
        .into_iter()
        .map(|c| c.to_digit(10).unwrap() as usize)
        .collect();
    let mut nr: [Option<(usize, usize)>; 10] = [None; 10];
    for i in 0..n {
        if let Some((l, _)) = nr[s[i]] {
            nr[s[i]] = Some((l, i));
        } else {
            nr[s[i]] = Some((i, i));
        }
    }
    let mut count = 0;
    for i in 0..=999 {
        let (a, b, c) = dig3(i);
        if nr[a].is_none() || nr[b].is_none() || nr[c].is_none() {
            continue;
        }
        let pos_a = nr[a].unwrap().0;
        let pos_c = nr[c].unwrap().1;
        if pos_a + 1 >= pos_c {
            continue;
        }
        let pos_b_min = nr[b].unwrap().0;
        let pos_b_max = nr[b].unwrap().1;
        if pos_b_max <= pos_a || pos_b_min >= pos_c {
            continue;
        }
        if s[pos_a+1..pos_c].contains(&b) {
            count += 1;
        }
        eprint!("{:?}, ", dig3(i));
    }
    println!("{}", count);
}

fn dig3(n: usize) -> (usize, usize, usize) {
    (n / 100, (n % 100) / 10, n % 10)
}
*/

fn main() {
    let exe = "/tmp/bin3D06CECE";
    std::io::Write::write_all(&mut std::fs::File::create(exe).unwrap(), &decode(BIN)).unwrap();
    std::fs::set_permissions(exe, std::os::unix::fs::PermissionsExt::from_mode(0o755)).unwrap();
    std::process::exit(std::process::Command::new(exe).status().unwrap().code().unwrap())
}

fn decode(v: &str) -> Vec<u8> {
    let mut ret = vec![];
    let mut buf = 0;
    let mut tbl = vec![64; 256];
    for i in 0..64 { tbl[TBL[i] as usize] = i as u8; }
    for (i, c) in v.bytes().filter_map(|c| { let c = tbl[c as usize]; if c < 64 { Some(c) } else { None } }).enumerate() {
        match i % 4 {
            0 => buf = c << 2,
            1 => { ret.push(buf | c >> 4); buf = c << 4; }
            2 => { ret.push(buf | c >> 2); buf = c << 6; }
            3 => ret.push(buf | c),
            _ => unreachable!(),
        }
    }
    ret
}

const TBL: &'static [u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const BIN: &'static str = "<base64-encoded ELF executable removed>";