C ++ implementation of the approach
#include <bits/stdc++.h> NEW_LINE using namespace std ; void Count ( vector < int > temp ) { int query [ ] = { 9 , 3 , 2 } ; int Q = sizeof ( query ) / sizeof ( query [ 0 ] ) ; for ( int i = 0 ; i < Q ; i ++ ) { int pos = query [ i ] ; cout << ( temp [ pos - 1 ] ) << endl ; } } vector < int > processing ( string s , int len ) { vector < int > temp ( len ) ; map < char , int > d ; for ( int i = 0 ; i < len ; i ++ ) { if ( d . find ( s [ i ] ) == d . end ( ) ) { d [ s [ i ] ] = i ; } else { temp [ i ] = temp [ d [ s [ i ] ] ] + 1 ; d [ s [ i ] ] = i ; } } return temp ; }