14,244,088 members
Rate this:
See more: , +
i want to compare two strings and calculate the percentage of difference between them. This could have been easier for two strings of same length. But what if the length is different? For eg :

hello my name is amit and i like to help everybody

hi my name is amit and i would like to help every coder

I want to calculate the percentage of difference between them

A simple logic would be to compare each word in first sentence with same position word of different sentence. But in this case the length is different. So how do i proceed?

Any help would be appreciated.
Posted

Rate this:

## Solution 2

Basically you need to decide on a string metric and then calculate the difference for your strings. There are many possible metrics, so it depends on how you want to define degrees of "different."

See here for start[^]. As mentioned, the Levenshtein metric is probably the most common.
Rate this:

## Solution 1

See here[^].

If you can get an array back (containing all the differences as is provided by the author here), you can calculate the percentage by calculating the differences versus the original string lengths.
v2

I will go through the code. But a little doubt : "An array of items containing the differences is returned." - This returns words from both the strings or only one string (former or later)? Or both is important to calculate the difference?