Levenshtein Distance in JavaScript

The Levenshtein distance is a string metric for measuring the difference between two sequences. It represents the minimum number of single-character edits (insertions, deletions, or substitutions) required to change one word into another.

Understanding Levenshtein Distance

Consider these two strings:

const str1 = 'hitting';
const str2 = 'kitten';

The Levenshtein distance between these strings is 3 because we need three edits:

  • kitten ? hitten (substitute "h" for "k")

  • hitten ? hittin (substitute "i" for "e")

  • hittin ? hitting (insert "g" at the end)

Algorithm Implementation

The algorithm uses dynamic programming with a 2D matrix to calculate the minimum edits:

const str1 = 'hitting';
const str2 = 'kitten';

const levenshteinDistance = (str1 = '', str2 = '') => {
   // Create a 2D array to store distances
   const track = Array(str2.length + 1).fill(null).map(() =>
      Array(str1.length + 1).fill(null));
   
   // Initialize first row (deletions from str1)
   for (let i = 0; i <= str1.length; i += 1) {
      track[0][i] = i;
   }
   
   // Initialize first column (insertions to str1)
   for (let j = 0; j <= str2.length; j += 1) {
      track[j][0] = j;
   }
   
   // Fill the matrix
   for (let j = 1; j <= str2.length; j += 1) {
      for (let i = 1; i <= str1.length; i += 1) {
         const indicator = str1[i - 1] === str2[j - 1] ? 0 : 1;
         track[j][i] = Math.min(
            track[j][i - 1] + 1,     // deletion
            track[j - 1][i] + 1,     // insertion
            track[j - 1][i - 1] + indicator, // substitution
         );
      }
   }
   
   return track[str2.length][str1.length];
};

console.log(levenshteinDistance(str1, str2));
3

How It Works

The algorithm builds a matrix where each cell [i][j] represents the minimum edits needed to transform the first i characters of str1 into the first j characters of str2. It considers three operations at each step:

  • Deletion: Remove a character from str1
  • Insertion: Add a character to str1
  • Substitution: Replace a character in str1

Additional Examples

// Test with different strings
console.log(levenshteinDistance('cat', 'bat'));      // 1 (substitute 'c' with 'b')
console.log(levenshteinDistance('hello', 'world'));  // 4 
console.log(levenshteinDistance('same', 'same'));    // 0 (identical strings)
console.log(levenshteinDistance('', 'abc'));         // 3 (insert 3 characters)
1
4
0
3

Common Use Cases

  • Spell checkers: Finding similar words for corrections
  • DNA analysis: Comparing genetic sequences
  • Search engines: Fuzzy matching for typos
  • Data deduplication: Identifying similar records

Conclusion

The Levenshtein distance algorithm efficiently calculates string similarity using dynamic programming. It's widely used in text processing, bioinformatics, and search applications where approximate string matching is needed.

Updated on: 2026-03-15T23:19:00+05:30

9K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements