-3

I come up an idea to read a byte[] array with the same size of the input, and check one by one. But it seems not very efficient. Is there a way to solve it by using rolling hash?

user6142261
  • 627
  • 1
  • 8
  • 14
  • do i get you right, you have an byte-array X and you have a file and you want to check if X is somewhere in the file data? – IEE1394 Nov 24 '17 at 23:18
  • 1
    Show some code and then ask a specific question – Abhijit Sarkar Nov 24 '17 at 23:19
  • Please see: https://stackoverflow.com/questions/1507780/searching-for-a-sequence-of-bytes-in-a-binary-file-with-java – dgumo Nov 24 '17 at 23:20
  • You can compute a rolling hash (Rabin Karp or some other ) , over a sliding window of size as byte array, over the entire file and check whether hash is the same. If same , then compare byte by byte. Since computing rolling hash is O(1), you will be able to solve the problem in O(file_size) . – akuriako Feb 20 '18 at 03:39

1 Answers1

0

If you are using java 8 or above please check the

java.util.Optional<T>

The documentation is here Optional If I got what you mean correctly