Why do you store the alphabet size separately? If the alphabet is changed, alphabetSize and alphabet.size() will differ and there is no way of updating alphabetSize.