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?
-
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– IEE13942017-11-24 23:18:26 +00:00Commented Nov 24, 2017 at 23:18
-
1Show some code and then ask a specific questionAbhijit Sarkar– Abhijit Sarkar2017-11-24 23:19:13 +00:00Commented Nov 24, 2017 at 23:19
-
Please see: stackoverflow.com/questions/1507780/…dgumo– dgumo2017-11-24 23:20:48 +00:00Commented Nov 24, 2017 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– akuriako2018-02-20 03:39:46 +00:00Commented Feb 20, 2018 at 3:39
Add a comment
|
1 Answer
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