What is the Big-O of String.contains() in Java?
One of the best known algorithms is the Boyer-Moore string searching algorithm which is O(n) although it can give sublinear performance in the best case.
Which algorithm is used in Java depends on which implemetation you download. It seems that for example OpenJDK uses a naive algorithm that runs in O(nm) and linear performance in the best case. See lines 1770-1806 here.
.contains()
definitely uses naive approach and equivalent to O(nm)
time complexity.
- Boyer-moore takes
O(nm)
time in the worst case. - KMP takes
O(n)
time in the worst case.
In one of the problems related to pattern matching, I used .contains()
and it took 70 ms
whereas replacing that line with patternSearch() //KMP search
reduced the time to 14 ms
.
Java source code | KMP search vs .contains()