I tried tinkering around with Robin-Karp algorithm but because we have to take the order into consideration, we would have to build a clever hash function that also takes the order into consideration. So we could maybe get it down to O(N).
I didn't try too hard but it also crossed my mind the same think that @aspittel
said, the size input is small and hence it won't really matter to the point of getting it resolved and proceed on AoC.
But just for curiosity sake I was planning to get back at that sometime later.
I have a JS implementation of this Robin-Karp algorithm as one of the exercises from "Cracking the Code Interview, 6th Ed", here you go:
// Robin-karp substring search with rolling hash functionfunctionsubstringSearch(string,substring){conststringLength=string.length;constsubstringLength=substring.length;// Generating hashesconsthashes=[calculateHash(string,substringLength)];for(leti=1;i<stringLength-substringLength;i++){hashes[i]=updateHash(string,substringLength,hashes[i-1],i-1);}constsubstringHash=calculateHash(substring,substringLength);// Comparing hashesfor(leti=0;i<stringLength-substringLength;i++){constindex=i+substringLength;if(hashes[i]===substringHash&&compare(string,i,substring)){returni;}}return-1;}functioncalculateHash(string,length,startIndex=0){lethash=0;for(leti=startIndex,power=length-1;i<length;i++,power--){hash+=string.charCodeAt(i)*Math.pow(128,power);}returnhash;}functionupdateHash(string,length,hash,outcomingIndex){return(hash-string.charCodeAt(outcomingIndex)*Math.pow(128,length-1))*128+string.charCodeAt(outcomingIndex+length);}functioncompare(string,index,substring){conststringLength=string.length;constsubstringLength=substring.length;if(index+substringLength>stringLength){returnfalse;}for(leti=index,j=0;i<stringLength&&j<substringLength;i++,j++){if(string.charAt(i)!==substring.charAt(j)){returnfalse;}}returntrue;}console.log(substringSearch('doe are hearing me','ear'));
Awesome! I will study this possibility later! I didn't know this algorithm and get back here tell how did I fare. The reason I am trying to make this < O(N2) is just because it seemed interesting not for some specific requisite of the problem, I imagined that the input wasn't big enough to justify all this effort.
Gotcha! It’s great to challenge ourselves to see if we can go one step beyond. Please let me know if you can find out something!
One limitation that I just thought with this algorithm is that it had been originally used to search subsequent characters inside a bigger string. But in our case, any char can be different, so we might need to look at the whole substring (which happens to have the same size as every other string) and hence maybe we won’t be able to get down to O(N).
But maybe there’s a way to create a very clever hash function that would work regardless of this limitation so we don’t need to manually compare each char. But I might be overlooking this.
Thanks!
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I have been also wrapping my head around that.
I tried tinkering around with Robin-Karp algorithm but because we have to take the order into consideration, we would have to build a clever hash function that also takes the order into consideration. So we could maybe get it down to O(N).
I didn't try too hard but it also crossed my mind the same think that @aspittel said, the size input is small and hence it won't really matter to the point of getting it resolved and proceed on AoC.
But just for curiosity sake I was planning to get back at that sometime later.
I have a JS implementation of this Robin-Karp algorithm as one of the exercises from "Cracking the Code Interview, 6th Ed", here you go:
Awesome! I will study this possibility later! I didn't know this algorithm and get back here tell how did I fare. The reason I am trying to make this < O(N2) is just because it seemed interesting not for some specific requisite of the problem, I imagined that the input wasn't big enough to justify all this effort.
Gotcha! It’s great to challenge ourselves to see if we can go one step beyond. Please let me know if you can find out something!
One limitation that I just thought with this algorithm is that it had been originally used to search subsequent characters inside a bigger string. But in our case, any char can be different, so we might need to look at the whole substring (which happens to have the same size as every other string) and hence maybe we won’t be able to get down to O(N).
But maybe there’s a way to create a very clever hash function that would work regardless of this limitation so we don’t need to manually compare each char. But I might be overlooking this.
Thanks!