2

I am learning about substring algorithms, esp Rabin Karp substring matching method from here and online sources. I see that for comparing longer substrings, we generally take the modulus.

  1. What are the characteristics of this K for the implementation to more effective?
  2. Why can't we compare String mod K as well as String div K for some K Why do we have to compare the entire string once a collision occurs? Isn't comparing the div and mod results going to be better than comparing the string?
  3. How do we modify the Rabin Karp Algorithm for String matching? Right now, I have implemented a method where each string is converted to it's ASCII value and stored in a Char Array. Is there a better way to do this?
  4. I undertsand how Inetegr.parseInt() is implemented and I see Java's String.Contains(). What algorithm is used in implementing this function?

Thank you!

Community
  • 1
  • 1
chipmunk
  • 545
  • 2
  • 9
  • 18
  • 1
    Re point 4, `String.contains` does not take a regex, just a literal string to search for, which it searches for using a naive, brute force, linear search. The source code comes with the JDK. – David Conrad Sep 05 '14 at 16:46
  • Sorry! Was meaning to ask about both Matches and contains. – chipmunk Sep 05 '14 at 16:47
  • @DavidConrad: Why does it not implement this algorithm? :o Isn't it better than brute force? – chipmunk Sep 05 '14 at 16:48
  • 1
    Algorithms like Rabin-Karp, Aho-Corasick, Boyer-Moore, and Knuth-Morris-Pratt have initialization overhead. They're useful when you can do the setup once and then run many searches, but for simple cases like `"algorithm".contains("go")` the start-up time would totally swamp any benefits. So they are rarely implemented as the string search algorithm in a standard library; instead, they are regulated to specialist libraries. I'm sure you can find Java libraries implementing all of them. – David Conrad Sep 05 '14 at 16:55

0 Answers0