Java的的indexOf(字符串str)方法的复杂性 [英] java indexof(String str) method complexity

查看:148
本文介绍了Java的的indexOf(字符串str)方法的复杂性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
  <一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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆