remember that better solution exists using suffix tree