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::io::*;
use std::str::FromStr;

pub fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("failed to read char") as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().expect("failed to parse token")
}

pub fn read_n<T: FromStr>(n: usize) -> Vec<T> {
    read_n_logic::<T>(n, vec![])
}

pub fn read_n_logic<T: FromStr>(n: usize, mut a: Vec<T>) -> Vec<T> {
    match n {
        0 => a,
        _ => {
            a.push(read::<T>());
            read_n_logic(n - 1, a)
        }
    }
}
use std::cmp::{max, min};

fn main() {
    let n: i64 = read();
    let s: String = read();
    let s_char: Vec<u32> = s.chars().map(|x| x.to_digit(10).unwrap()).collect();
    let mut ans = 0;

    for i in 0..1000 {
       let first_digit = if i < 100 { 0 } else { i / 100 };
       let second_digit = if i < 10 { 0 } else { i % 100 / 10 };
       let third_digit = i % 10;

       let f_index: usize = match s_char.iter().position(|x| x == &first_digit) {
           Some(s) => s,
           None => continue
       };
       let s_index: usize = match s_char[f_index+1..].iter().position(|x| x == &second_digit) {
           Some(s) => s,
           None => continue
       };
       match s_char[f_index+1..][s_index+1..].iter().position(|x| x == &third_digit) {
           Some(s) => ans += 1,
           None => continue
       }
    }


    println!("{}", ans);
}