It is not possible to perform any operations with String
without using a library function. For example, your code uses String.toCharArray
. And if you can use this, you can also use String.indexOf
and avoid wheel reuse.
People suggested Boyer-Moore. This is a good choice if you intend to search for large text (in String
instances or in another representation). However, if you are going to look for a small piece of text (as in your question), then the cost of installing Boyer-Moore means that String.indexOf()
will be faster. The same applies to other complex algorithms.
So, the only way this question makes sense is that this is a homework exercise that includes a restriction on what you are allowed to use to solve the problem. In this case, if you do not complete the course of algorithms, I doubt that they expect you to study and implement a complex algorithm.
source share