function dist = levenshtein (str1, str2, weights)
  if(nargin < 2 || (nargin == 3 && length(weights)  < 3) )
    print_usage();
  end

  L1=length(str1)+1;
  L2=length(str2)+1;

  if L1 == 1 dist = L2-1; return;
  elseif L2 == 1 dist = L1-1; return;
  endif

  L=zeros(2,L2);

  if(nargin < 3)
    g=+1;%insertion
    m=+0;%match
    d=+1;%deletion
  else
    g=weights(1);
    m=weights(2);
    d=weights(3);
  end

#  L(:,1)=[0:L1-1]'*g;
  L(1,:)=[0:L2-1]*g;

  m4=0;
  for idx=2:L1;
  	L(2,1) = idx-1;
    for idy=2:L2
      if(str1(idx-1)==str2(idy-1))
        score=m;
      else
        score=d;
      end
      m1=L(1,idy-1) + score;
      m2=L(1,idy) + g;
      m3=L(2,idy-1) + g;
      L(2,idy)=min(m1,min(m2,m3));
    end
    L(1,:) = L(2,:);
  end

  dist=L(2,L2);
endfunction
