Hmm I think alghoritm it's not bad but I'm little tried and maybe I don't see something. When I put input from my coment result is exact, but when I copy from conditions: Example of file contents rat tar tart a a tot tot tot Result is with some squares, I think it's different format, I try I little combine with StandardCharsets.UTF_8 and getBytes() method but I don't see any difference. Maybe someone know how to do it. PS: I know I maybe should spent more time on research, but in other side I don't see when I peek on help site, that anyone do something with setting charsets to UTF-8 (but I only peek).