Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function lz78_encode(str){
- let d=[], s="", ind=-1, i, j, dcom=[];
- for(i=0; i<str.length; i++){
- s+=str[i];
- for(j=0; j<d.length; j++){
- if( s==dcom[j] ){
- ind=j;
- break;
- }
- }
- if( j==d.length ){
- d.push({index: ind+1, char: str[i]});
- dcom.push(((ind==-1)?"":dcom[ind])+str[i]);
- s="";
- ind=-1;
- }else if( i+1==str.length ){
- d.push({index: ind+1, char: ""});
- }
- }
- return d;
- }
- function lz78_decode(d){
- let res="", str=[];
- for(let i=0; i<d.length; i++){
- if( d[i].index==0 ){
- res+=d[i].char;
- str.push(d[i].char);
- }else{
- res+=str[d[i].index-1]+d[i].char;
- str.push(str[d[i].index-1]+d[i].char);
- }
- }
- return res;
- }
Advertisement
Add Comment
Please, Sign In to add comment