Finding how similar two strings are
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Determining the similarity between two strings is a fundamental problem in computer science with wide applications, ranging from spell checking and plagiarism detection to DNA sequence alignment. Various algorithms and techniques can measure string similarity, each suited for different types of problems and datasets. This article provides an in-depth exploration of these methods along with technical explanations and examples.
Measuring String Similarity
Edit Distance
One of the most commonly used techniques is the Edit Distance, also known as Levenshtein Distance. This metric counts the minimum number of operations required to transform one string into another. The allowable operations include insertions, deletions, or substitutions of a single character.
Example
Consider the transformation from "kitten" to "sitting":
- kitten → sitten (substitution of 'k' with 's')
- sitten → sittin (substitution of 'e' with 'i')
- sittin → sitting (insertion of 'g')
The edit distance here is 3.
Formula:
For two strings and , the Levenshtein distance can be recursively defined as:
where if , else .
Cosine Similarity
Cosine Similarity is particularly useful in high-dimensional spaces. It measures the cosine of the angle between two non-zero vectors of an inner product space. This method is especially popular in comparing documents in text analysis.
Formula:
Given two vectors $\mathbf\{A\}$ and $\mathbf\{B\}$, the cosine similarity is:
Here, denotes the dot product, and , are the magnitudes of the vectors.
Jaccard Similarity
The Jaccard Index, also known as Intersection over Union, is ideal for comparing the similarity and diversity of sample sets. It measures the size of the intersection divided by the size of the union of the data sets.
Formula:
Given two sets and , the Jaccard similarity is:
Applications
• Spell Checkers: Implement edit distance algorithms to suggest corrections for misspelled words. • Plagiarism Detection: Use cosine similarity to verify the similarity between large documents. • DNA Sequencing: Levenshtein distance is applied to measure differences or mutations between DNA sequences.
Summary Table
The following table summarizes the discussed string similarity measures:
| Method | Metric Type | Suitable For | Complexity |
| Edit Distance | Transformations | Short Strings | |
| Cosine Similarity | Geometric Distance | High-Dimensional Space | with sparse vector |
| Jaccard Similarity | Set Theory | Sets, Text Data |
Conclusion
Understanding and computing string similarity is essential for numerous applications in computational linguistics, bioinformatics, and data science. Selecting the appropriate metric depends on the nature of the data and the specific requirements of the task at hand. Whether you're aligning DNA sequences or comparing lengthy documents, the techniques described provide a robust foundation for measuring and analyzing string similarity.
Related reading
- Finding meaningful sub-sentences from a sentence
- Focused Named Entity Recognition NER?
- Function that returns affinity between texts?
- Fuzzy search algorithm approximate string matching algorithm
- Finding if a Binary Tree is a Binary Search Tree
- Finding if a string is an iterative substring Algorithm in C?
- Fuzzy text sentences/titles matching in C
- General Address Parser for Freeform Text

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.