How to calculate distance similarity measure of given 2 strings?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
There is no single string "distance" or "similarity" measure that is best for every problem. The right metric depends on what kinds of differences matter: character typos, swapped words, missing tokens, or approximate phonetic matches. A good answer starts by matching the metric to the error pattern in your data instead of blindly picking one formula.
Levenshtein Distance Is the Default for Typo-Like Errors
Levenshtein distance counts the minimum number of insertions, deletions, and substitutions needed to change one string into another. It is a strong default when you care about spelling-like edits.
This gives a distance, not a similarity score. Lower is more similar.
Normalize If Lengths Differ a Lot
Raw edit distance is hard to compare across strings of very different lengths. Turning it into a normalized score is often more useful for thresholds.
This produces a score between 0 and 1, where higher means more similar.
Token-Based Similarity Helps with Word Reordering
If you are comparing names, titles, or short phrases, token overlap may matter more than exact character position. Jaccard similarity is a simple token-based option.
This ignores repeated words and token order, which is sometimes exactly what you want.
Character N-Gram Similarity Handles Noisy Partial Matches
For noisy text where exact tokens are unreliable, character n-grams often work well. One common choice is cosine similarity over character bigrams or trigrams.
This often captures "looks similar" better than raw token overlap.
Pick the Metric Based on the Task
A practical rule:
- use Levenshtein for spelling mistakes and short string edits
- use Jaccard for bag-of-words style overlap
- use n-gram cosine when you want softer partial matching
If you are building production matching logic, test several metrics on labeled examples rather than assuming one metric is universally correct.
Thresholds Matter as Much as the Formula
A similarity score is only useful if you know what score counts as "close enough". That threshold depends on your error tolerance.
Examples:
- customer search may favor recall
- identity matching may favor precision
- deduplication may need a review band rather than one hard cutoff
So do not copy thresholds from another dataset without validation.
Common Pitfalls
- Treating one metric as universally best across all domains.
- Comparing raw edit distance across strings of very different lengths.
- Forgetting preprocessing such as lowercasing, whitespace normalization, or tokenization.
- Using token overlap when character-level typos are the main problem.
- Confusing lexical similarity with semantic equivalence.
Summary
- String distance and similarity measures must match the kind of errors you expect.
- Levenshtein is a strong choice for typo-like character edits.
- Jaccard is useful for token overlap problems.
- Character n-gram cosine similarity handles softer partial matches well.
- Good thresholds come from validation data, not from guesswork.

