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::cmp::*;

#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let lock = stdin.lock();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(lock));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        eprintln!(concat!($(stringify!($a), " = {:?}, "),*), $($a),*);
    }
}

#[allow(unused_macros)]
macro_rules! max {
    ($x:expr) => {
      $x
    };
    ($x:expr, $($xs:tt)+) => {
      max($x,max!($($xs)+))
    };
  }

#[allow(unused_macros)]
macro_rules! min {
    ($x:expr) => {
      $x
    };
    ($x:expr, $($xs:tt)+) => {
      min($x,min!($($xs)+))
    };
  }

use std::ascii::AsciiExt;
use std::collections::HashMap;
use std::collections::VecDeque;
use std::io::Write;
use std::iter::Iterator;

use std::fmt;
use std::ops::{Add, Div, Mul, Sub};

#[allow(dead_code)]
const MODLL: i64 = 1000_000_007;

#[derive(Copy, Clone)]
struct Modulo {
    n: i64,
}

// precondition: p is prime and mod
fn mod_inverse(a: i64, p: i64) -> i64 {
    if a == 1 {
        1
    } else {
        (1 - p * mod_inverse(p % a, a)) / a + p
    }
}

#[allow(dead_code)]
impl Modulo {
    fn new(n: i64) -> Modulo {
        Modulo {
            n: (n % MODLL + MODLL) % MODLL,
        }
    }

    fn inv(&self) -> Modulo {
        Modulo {
            n: mod_inverse(self.n, MODLL),
        }
    }
}

impl fmt::Debug for Modulo {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.n)
    }
}

impl Add for Modulo {
    type Output = Modulo;
    fn add(self, other: Modulo) -> Modulo {
        Modulo {
            n: (self.n + other.n) % MODLL,
        }
    }
}
impl Add<i32> for Modulo {
    type Output = Modulo;
    fn add(self, other: i32) -> Modulo {
        Modulo {
            n: (self.n + other as i64) % MODLL,
        }
    }
}
impl Add<i64> for Modulo {
    type Output = Modulo;
    fn add(self, other: i64) -> Modulo {
        Modulo {
            n: (self.n + other) % MODLL,
        }
    }
}
impl Sub for Modulo {
    type Output = Modulo;
    fn sub(self, other: Modulo) -> Modulo {
        Modulo {
            n: (self.n - other.n + MODLL) % MODLL,
        }
    }
}
impl Sub<i32> for Modulo {
    type Output = Modulo;
    fn sub(self, other: i32) -> Modulo {
        self - Modulo::new(other as i64)
    }
}
impl Sub<i64> for Modulo {
    type Output = Modulo;
    fn sub(self, other: i64) -> Modulo {
        self - Modulo::new(other)
    }
}
impl Mul for Modulo {
    type Output = Modulo;
    fn mul(self, other: Modulo) -> Modulo {
        Modulo {
            n: (self.n * other.n) % MODLL,
        }
    }
}
impl Mul<i32> for Modulo {
    type Output = Modulo;
    fn mul(self, other: i32) -> Modulo {
        self * Modulo::new(other as i64)
    }
}
impl Mul<i64> for Modulo {
    type Output = Modulo;
    fn mul(self, other: i64) -> Modulo {
        self * Modulo::new(other)
    }
}
impl Div for Modulo {
    type Output = Modulo;
    fn div(self, other: Modulo) -> Modulo {
        self * other.inv()
    }
}
impl Div<i32> for Modulo {
    type Output = Modulo;
    fn div(self, other: i32) -> Modulo {
        self * mod_inverse(other as i64, MODLL)
    }
}
impl Div<i64> for Modulo {
    type Output = Modulo;
    fn div(self, other: i64) -> Modulo {
        self * mod_inverse(other, MODLL)
    }
}

struct Combination {
    fact: Vec<Modulo>,
    ifact: Vec<Modulo>,
}

#[allow(dead_code)]
impl Combination {
    fn new(n: usize) -> Combination {
        let mut fact = vec![Modulo::new(1); n + 1];
        let mut ifact = vec![Modulo::new(1); n + 1];
        for i in 1..n + 1 {
            fact[i] = fact[i - 1] * i as i64;
            ifact[i] = ifact[i - 1] / i as i64;
        }
        Combination {
            fact: fact,
            ifact: ifact,
        }
    }

    /// n!
    fn fact(&self, n: usize) -> Modulo {
        self.fact[n]
    }
    /// 1/n!
    fn inv_fact(&self, n: usize) -> Modulo {
        self.ifact[n]
    }
    /// nCr
    fn comb(&self, n: usize, r: usize) -> Modulo {
        if r > n {
            Modulo::new(1)
        } else {
            self.fact(n) * self.inv_fact(n - r) * self.inv_fact(r)
        }
    }
}

#[derive(PartialEq, Debug, Clone)]
pub struct UnionFindNode {
    pub par: usize,
    pub rank: usize,
}

#[derive(PartialEq, Debug, Clone)]
pub struct UnionFind(pub Vec<UnionFindNode>);

impl UnionFind {
    //初期化
    pub fn new(size: usize) -> UnionFind {
        let mut vec = Vec::new();
        for i in 0..size {
            vec.push(UnionFindNode { par: i, rank: 0 });
        }

        UnionFind(vec)
    }

    //根を求める
    pub fn find(&mut self, x: usize) -> usize {
        if self.0[x].par == x {
            x
        } else {
            let par = self.0[x].par;
            let v = self.find(par);
            self.0[x].par = v;
            v
        }
    }

    //xとyの集合を併合
    pub fn unite(&mut self, x: usize, y: usize) {
        let x = self.find(x);
        let y = self.find(y);
        if x == y {
            return;
        }

        if self.0[x].rank < self.0[y].rank {
            self.0[x].par = y;
        } else {
            self.0[y].par = x;
            if self.0[x].rank == self.0[y].rank {
                self.0[x].rank += 1;
            }
        }
    }

    //xとyが同じ集合に属するか
    pub fn same(&mut self, x: usize, y: usize) -> bool {
        self.find(x) == self.find(y)
    }
}

pub trait LexicalPermutation {
    fn next_permutation(&mut self) -> bool;
    fn prev_permutation(&mut self) -> bool;
}

impl<T> LexicalPermutation for [T]
where
    T: Ord,
{
    fn next_permutation(&mut self) -> bool {
        if self.len() < 2 {
            return false;
        }
        let mut i = self.len() - 1;
        while i > 0 && self[i - 1] >= self[i] {
            i -= 1;
        }
        if i == 0 {
            return false;
        }
        let mut j = self.len() - 1;
        while j >= i && self[j] <= self[i - 1] {
            j -= 1;
        }
        self.swap(j, i - 1);
        self[i..].reverse();
        true
    }
    fn prev_permutation(&mut self) -> bool {
        if self.len() < 2 {
            return false;
        }
        let mut i = self.len() - 1;
        while i > 0 && self[i - 1] <= self[i] {
            i -= 1;
        }
        if i == 0 {
            return false;
        }
        self[i..].reverse();
        let mut j = self.len() - 1;
        while j >= i && self[j - 1] < self[i - 1] {
            j -= 1;
        }
        self.swap(i - 1, j);
        true
    }
}

//(小さい,同じ,大きい)
pub fn bound_count<T: Ord>(vec: &Vec<T>, val: &T) -> (usize, usize, usize) {
    let lb = lower_bound(vec, 0, vec.len(), val);
    let ub = upper_bound(vec, 0, vec.len(), val);
    (lb, ub - lb, vec.len() - ub)
}

//より小さい
pub fn less_count<T: Ord>(vec: &Vec<T>, val: &T) -> usize {
    lower_bound(vec, 0, vec.len(), val)
}

//より大きい
pub fn larger_count<T: Ord>(vec: &Vec<T>, val: &T) -> usize {
    vec.len() - upper_bound(vec, 0, vec.len(), val)
}

//同じ
pub fn eq_count<T: Ord>(vec: &Vec<T>, val: &T) -> usize {
    upper_bound(vec, 0, vec.len(), val) - lower_bound(vec, 0, vec.len(), val)
}

//vecは昇順ソート済み
//以上
pub fn lower_bound<T: Ord>(vec: &Vec<T>, mut first: usize, mut last: usize, val: &T) -> usize {
    let mut mid;
    while last - first > 1 {
        mid = (first + last) / 2;
        if &vec[mid] < val {
            first = mid;
        } else {
            last = mid;
        }
    }
    if &vec[first] < val {
        last
    } else {
        first
    }
}

//より大きい
pub fn upper_bound<T: Ord>(vec: &Vec<T>, mut first: usize, mut last: usize, val: &T) -> usize {
    let mut mid;
    while last - first > 1 {
        mid = (first + last) / 2;
        if &vec[mid] <= val {
            first = mid;
        } else {
            last = mid;
        }
    }
    if &vec[first] <= val {
        last
    } else {
        first
    }
}

use std::io::*;
use std::str::*;

fn read<T: FromStr>() -> Option<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()
}

fn main() {
    input! {
        n:usize,
        s:chars,
    }

    let mut cnt = 0;
    for i in 0..1000 {
        let target: Vec<char> = format!("{0:>03}", i).chars().collect();
        let mut matchflg = true;
        let mut index = 0;
        for ss in target {
            
            if let Some(index2) = s[index..].iter().position(|x| *x == ss) {
                index = index+index2 + 1;
            } else {
                matchflg = false;
                break;
            }
        }

        if !matchflg {
            continue;
        } else {
            cnt += 1;
        }
    }

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