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::str::FromStr;
use std::fmt::Debug;
use std::cmp;

#[allow(dead_code)]
fn read_as_vec<T>() -> Vec<T>
where T: FromStr, <T as FromStr>::Err : Debug{
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).unwrap();
    s.trim().split_whitespace().map(|c| T::from_str(c).unwrap()).collect()

}

#[allow(dead_code)]
fn read_as_string() -> String{
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).unwrap();
    s.trim().to_string()
}


//return pos more than m
fn bin_search(v: &Vec<usize>, m: usize) -> usize{

    if v.len() == 0{
        return std::usize::MAX;
    }

    // (left..right]
    let mut left = 0;
    let mut right = v.len()-1;

    while left < right{
        let mid = (left+right)/2;
        //println!("left = {}, right = {}, mid = {}", left, right, mid);
        if v[mid] < m{
            left = mid + 1;
        }else{
            right = mid;
        }
    }
    if v[right] < m{
        return std::usize::MAX;
    }else{
        return v[right];
    }
}

fn main(){
    /*
    let v = vec![10];
    let hoge = bin_search(&v, 10);
    println!("{}", hoge);
    return;
     */

    let n: usize = read_as_vec()[0];
    let s = read_as_string();

    let mut ans_v = vec![vec![vec![false;10];10];10];
    let mut v = vec![vec![];10];

    for (pos, c) in s.bytes().enumerate(){
        v[(c-48) as usize].push(pos);
    }


    //println!("{:?}", v);
    let dummy = std::usize::MAX;
    //first digit
    for i in 0..10{
        //eprintln!("\ni = {}", i);
        let first_digit_pos = bin_search(&v[i], 0);
        //println!("first_digit_pos = {}", first_digit_pos);
        if dummy == first_digit_pos{
            continue;
        }
        //second digit
        for j in 0..10{
            //println!("--j = {}--", j);
            let second_digit_pos = bin_search(&v[j], first_digit_pos+1);
            //println!("second_digit_pos = {}", second_digit_pos);
            if dummy == second_digit_pos{
                continue;
            }
            for k in 0..10{

                if std::usize::MAX != bin_search(&v[k], second_digit_pos+1){
                    ans_v[i][j][k] = true;
                }
            }
        }


    }


    let mut ans = 0;
    for i in 0..10{
        for j in 0..10{
            for k in 0..10{
                if ans_v[i][j][k]{
                    //println!("true: {}{}{}", i, j, k);
                    ans += 1;
                }
            }
        }
    }

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