We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
// Construction of Z array keeping the first element equal to length of stringfunctionzArrayHelper(s){s=s.toLowerCase();letZ=[s.length];letn=s.length;let[L,R]=[0,0];letk;for(leti=1;i<n;i++){if(i>R){[L,R]=[i,i];while(R<n&&s[R-L]==s[R]){R++;}Z[i]=R-L;R--;}else{k=i-L;if(Z[k]<R+1-i){Z[i]=Z[k];}else{L=i;while(R<n&&s[R-L]==s[R]){R++;}Z[i]=R-L;R--;}}}returnZ;}// Returns the sum of length of all the matching longest common prefixfunctionstringSimilarity(s){// Write your code herereturnzArrayHelper(s).reduce((sum,length)=>sum+length,0);}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
String Similarity
You are viewing a single comment's thread. Return to all comments →
JavaScript solution