Levenshtein fail-fast if distance > max
===

For the Leveshein algorithm I have found [this implementation for Delphi][1]. 

I need a version which stops as soon as a maximum distance is hit, and return the distance found so far.

My first idea is to check the current result after every iteration:


    for i := 1 to n do
        for j := 1 to m do
        begin
          d[i, j] := Min(Min(d[i-1, j]+1, d[i,j-1]+1), d[i-1,j-1]+Integer(s[i] <> t[j]));
    
          // check   
          Result := d[n, m];
          if Result > max then
          begin
            Exit;
          end; 
    
        end;

Would this be a valid variation of the algorithm to get a fail-fast behavior?  


  [1]: http://stackoverflow.com/questions/10580241/how-can-i-compute-a-difference-between-two-strings/10593797#10593797