Detect differences between two strings
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
In the realm of programming and data analysis, detecting differences between two strings is a common task. This can be crucial in various applications, such as text comparison, plagiarism detection, version control, and data validation. In this article, we'll explore techniques to find differences between two strings, delve into technical explanations of algorithms like diff and Levenshtein distance, and present practical examples.
String Comparison Basics
String comparison can be as simple as checking for equality or as complex as highlighting the nuanced differences between two strings. The complexity arises from the need to identify not just if two strings are different but to pinpoint how they differ.
Key Techniques
- Equality Check: Directly checks whether two strings are the same. • Example:
"hello" == "hello"results inTrue. - Lexicographical Comparison: Compares strings based on alphabetical order. • Example:
"apple" < "banana"results inTrue. - Diff Algorithms: These are more advanced mechanisms that not only show that strings differ but also highlight precise changes. • Unix's
diffutility is a classic example. - Levenshtein Distance: Measures how many single-character edits (insertions, deletions, substitutions) are required to change one string into another.
Technical Explanation of Algorithms
Diff Algorithm
The diff algorithm works by finding the longest common subsequence (LCS) between two strings and then noting the differences around this subsequence. Here’s a basic illustration with strings:
• String 1: "kitten" • String 2: "sitting"
The LCS here is "itt". Changes needed: • Substitute "k" with "s" • Substitute "e" with "i" • Append "ing"
The diff
tool outputs these differences, typically in a format that Unix users are familiar with (-
means delete, +
means add):
• kitte
• Initialize a matrix d
of size .
• Set base cases: and .
• For each calculate:
1 + \min { d(i-1, j), d(i, j-1), d(i-1, j-1) } & \text{otherwise}
• String 1: "kitten"
• String 2: "sitting"
• k • e
• Git Diff: Commonly used in software development, providing visual and contextual differences between files. • String Interpolators: Used in templating and translation systems to highlight missing or mismatched variables.
Related reading
- Difference between feature_column.embedding_column and keras.layers.Embedding in TensorFlow
- difference between Latent and Explicit Semantic Analysis
- Difference between solr and lucene
- Difference in values of tf-idf matrix using scikit-learn and hand calculation
- Detect when a graph has been broken into two or more connected components
- Detecting consecutive integers in a list
- Do Not Embed, `Embed` Sign, `Embed` Without Signing. What are they?. What they do?
- Document similarity Vector embedding versus Tf-Idf performance?

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.