Store the frequency of the characters
for i in range ( len ( A ) ) : NEW_LINE INDENT S [ ord ( A [ i ] ) - ord ( ' a ' ) ] . append ( i ) NEW_LINE DEDENT low = 0 NEW_LINE for i in range ( len ( B ) ) : NEW_LINE INDENT indexes = S [ ord ( B [ i ] ) - ord ( ' a ' ) ] NEW_LINE it = lower_bound ( indexes , low ) NEW_LINE DEDENT