Compare Strings in JavaScript and return percentage of likeliness

We are required to write a JavaScript function that can compare two strings and return the percentage likeliness of how much they are alike. The percentage will be nothing but a measure of many characters the two strings have in common.

If they are completely similar the output should be 100, and if they contain no common character at all, the output should be 0.

Understanding the Algorithm

This implementation uses the Levenshtein distance algorithm to calculate string similarity. The Levenshtein distance measures the minimum number of single-character edits (insertions, deletions, or substitutions) needed to change one string into another.

Example

const calculateSimilarity = (str1 = '', str2 = '') => {
    let longer = str1;
    let shorter = str2;
    if (str1.length  {
    str1 = str1.toLowerCase();
    str2 = str2.toLowerCase();
    let arr = new Array();
    for (let i = 0; i  0){
                let newValue = arr[j - 1];
                if(str1.charAt(i - 1) != str2.charAt(j - 1))
                    newValue = Math.min(Math.min(newValue, lastValue), arr[j]) + 1;
                arr[j - 1] = lastValue; 
                lastValue = newValue;
            }
        }
        if (i > 0) arr[str2.length] = lastValue;
    }
    return arr[str2.length];
};

console.log(calculateSimilarity('Mathematics','Mathamatecs'));
console.log(calculateSimilarity('hello','world'));
console.log(calculateSimilarity('JavaScript','JavaScript'));
console.log(calculateSimilarity('test',''));

Output

72.73
20
100
0

How It Works

The algorithm works in the following steps:

  1. Identify longer and shorter strings: This ensures consistent calculation regardless of parameter order.
  2. Handle edge cases: If both strings are empty, similarity is 100%.
  3. Calculate Levenshtein distance: Uses dynamic programming to find the minimum edit distance.
  4. Convert to percentage: Formula: (longerLength - distance) / longerLength × 100

Alternative Simple Implementation

Here's a simpler approach using character frequency comparison:

const simpleSimilarity = (str1, str2) => {
    if (str1 === str2) return 100;
    if (str1.length === 0 && str2.length === 0) return 100;
    if (str1.length === 0 || str2.length === 0) return 0;
    
    const longer = str1.length > str2.length ? str1 : str2;
    const shorter = str1.length > str2.length ? str2 : str1;
    
    let matches = 0;
    const shorterLower = shorter.toLowerCase();
    const longerLower = longer.toLowerCase();
    
    for (let char of shorterLower) {
        if (longerLower.includes(char)) {
            matches++;
        }
    }
    
    return Math.round((matches / longer.length) * 100);
};

console.log(simpleSimilarity('hello', 'hallo')); 
console.log(simpleSimilarity('cat', 'dog'));     
console.log(simpleSimilarity('test', 'test'));   

Output

80
0
100

Comparison

Method Accuracy Performance Use Case
Levenshtein Distance High O(m×n) Precise similarity measurement
Character Frequency Moderate O(m+n) Quick approximate similarity

Conclusion

The Levenshtein distance approach provides accurate string similarity percentages by calculating edit distance. For simpler needs, character frequency comparison offers a faster alternative with reasonable accuracy.

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

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements