Longest Duplicate Substring | Rabin Karp Rolling Hash | Text Processing

preview_player
Показать описание




.
.
.
Happy Programming !!! Pep it up 😍🤩
.
.
.
#pepcoding #code #coder #codinglife #programming #coding #java #freeresources #datastrucutres #pepcode #competitive #competitiveprogramming #softwareengineer #engineering #engineer
Рекомендации по теме
Комментарии
Автор

IN Robin Karp video, you took 'power' variable and 'p' variable 31. Here they are 31 and 1 resp. Any special reason?

AmitSharma-wxwj
Автор

class Solution {
public:
string check(string s, int len){
unordered_set<long int>set;
long int n=s.size();
long int p=31;
long int ch=0;
long int mp=1;
long int m=1e9+7;
for(int i=len-1;i>=0;i--){
ch =(((s[i]-'a'+1)*mp)%m +ch)%m;
if(i>0)mp=(mp*p)%m;
}
set.insert(ch);
for(int i=len;i<n;i++){
ch= (ch-(((s[i-len]-'a'+1)*mp)%m) +m)%m;
ch =(ch*p)%m;
ch=(ch+(s[i]-'a'+1))%m;

s.substr(i-len+1, len);
set.insert(ch);
}
return "";
}
string longestDupSubstring(string s) {
int n=s.size();
int l=0;
int r=n;
string ans="";
while(l<=r){
int m=l+(r-l)/2;
string cs=check(s, m);

if(cs.size()>0){
ans=cs;
l=m+1;
}
else{
r=m-1;
}
//cout<<cs<<"=="<<m<<" "<<l<<" "<<r<<endl;
}
return ans;
}
};



plz tell why not all test cases passing on leetcode

raghavsinghhal
Автор

please dont use dark theme while coding...light theme is best for making tutorials, otherwise most people don't get what you're typing.

amanpatel