Java的的indexOf(字符串str)方法的复杂性 [英] java indexof(String str) method complexity
问题描述
可能重复:
<一href="http://stackoverflow.com/questions/3562919/what-is-the-cost-complexity-of-a-string-indexof-function-call">What是String.indexof()函数调用的成本/复杂性
什么是Java的的indexOf(字符串str)方法的复杂性。我的意思是有字符串匹配算法类似KMP其中以线性时间运行。我实现,需要寻找大子在一个真正的大串的系统,这样我就可以用java的indexOf(字符串str)方法,或者我应该实现KMP。
What is the complexity of java indexof(String str) method. I mean there are String matching algorithms like KMP which runs in linear time. I am implementing a system that needs to search for large substring in a really large string,so can I use java indexof(String str) method or I should to implement KMP.
推荐答案
Java的的的的实施 的indexOf
是 O(M * N)
,其中 N
和 M
分别是搜索字符串和模式的长度。
The complexity of Java's implementation of indexOf
is O(m*n)
where n
and m
are the length of the search string and pattern respectively.
你能做些什么来改善复杂性是使用,例如,在博耶-更多算法智能跳过比较其无法比拟的模式字符串的逻辑部分。
What you can do to improve complexity is to use e.g., the Boyer-More algorithm to intelligently skip comparing logical parts of the string which cannot match the pattern.
这篇关于Java的的indexOf(字符串str)方法的复杂性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!