Add isPrefixOf helper, algorithm now terminates for all cases.