Python 3 implementation of the approach
def Count ( temp ) : NEW_LINE INDENT query = [ 9 , 3 , 2 ] NEW_LINE Q = len ( query ) NEW_LINE for i in range ( Q ) : NEW_LINE INDENT pos = query [ i ] NEW_LINE print ( temp [ pos - 1 ] ) NEW_LINE DEDENT DEDENT def processing ( s ) : NEW_LINE INDENT temp = [ 0 ] * len ( s ) NEW_LINE d = dict ( ) NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT if s [ i ] not in d : NEW_LINE INDENT d [ s [ i ] ] = i NEW_LINE DEDENT else : NEW_LINE INDENT temp [ i ] = temp [ d [ s [ i ] ] ] + 1 NEW_LINE d [ s [ i ] ] = i NEW_LINE DEDENT DEDENT return temp NEW_LINE DEDENT