string similarity
text comparison
string matching
algorithm
natural language processing

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.

Practice ML system design

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":

  1. kitten → sitten (substitution of 'k' with 's')
  2. sitten → sittin (substitution of 'e' with 'i')
  3. sittin → sitting (insertion of 'g')

The edit distance here is 3.

Formula:

For two strings aa and bb, the Levenshtein distance dd can be recursively defined as:

d(i,j)={0if i=0 and j=0,iif j=0,jif i=0,min{d(i1,j)+1d(i,j1)+1d(i1,j1)+costotherwise\begin{align*} d(i, j) = \begin{cases} 0 & \text{if } i = 0 \text{ and } j = 0, \\ i & \text{if } j = 0, \\ j & \text{if } i = 0, \\ \min \begin{cases} d(i-1, j) + 1 \\ d(i, j-1) + 1 \\ d(i-1, j-1) + \text{cost} \end{cases} & \text{otherwise} \end{cases} \end{align*}

where cost=0\text{cost} = 0 if a[i]=b[j]a[i] = b[j], else cost=1\text{cost} = 1.

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:

similarity=cos(θ)=ABAB\text{similarity} = \cos(\theta) = \frac{\mathbf{A} \cdot \mathbf{B}}{|\mathbf{A}| |\mathbf{B}|}

Here, AB\mathbf{A} \cdot \mathbf{B} denotes the dot product, and A\|\mathbf{A}\|, B\|\mathbf{B}\| 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 AA and BB, the Jaccard similarity is:

J(A,B)=ABABJ(A, B) = \frac{|A \cap B|}{|A \cup B|}

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:

MethodMetric TypeSuitable ForComplexity
Edit DistanceTransformationsShort StringsO(n×m)O(n \times m)
Cosine SimilarityGeometric DistanceHigh-Dimensional SpaceO(n)O(n) with sparse vector
Jaccard SimilaritySet TheorySets, Text DataO(min(lvertArvert,lvertBrvert))O(\min(\\lvert A \\rvert, \\lvert B \\rvert))

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
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice ML system design

All Rights Reserved.