Change strings to make them equal
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
“Change strings to make them equal” is not one algorithmic problem. It is a family of problems whose answer depends on the allowed operations. If you are allowed only replacement, the solution is simple. If insertion and deletion are allowed too, you need edit distance.
Start by Defining the Operation Model
Before writing code, answer one question: what counts as one change?
Common versions are:
- replace one character
- insert one character
- delete one character
- swap adjacent characters
- use weighted costs instead of cost
1
If you skip this step, it is easy to build an algorithm that is correct for the wrong problem.
For example, with equal-length strings and replacement only, "abc" to "axc" needs one change. If insertions and deletions are allowed, the same answer happens to be one, but that is not always true for longer examples.
Equal-Length Strings With Replacement Only
If the strings have the same length and replacement is the only operation, count mismatched positions.
This runs in O(n) time and O(1) extra space. It is the right answer for many interview questions that look more complex than they actually are.
A shorter version using a generator is fine too:
General String Changes With Edit Distance
If insertions, deletions, and substitutions are allowed, the standard solution is Levenshtein distance. Dynamic programming works because each prefix pair depends on smaller prefix pairs.
This solution is O(n * m) in time and space for string lengths n and m.
Space Optimization for Large Inputs
If the strings are long, the full matrix may be unnecessarily expensive. You only need the previous row and the current row at any step.
This still takes O(n * m) time but uses much less memory.
Choose the Right Algorithm for the Real Task
Many production use cases are not exactly Levenshtein distance either. Spell-checking, entity matching, and typo correction often need a mix of heuristics, normalization, and search pruning before distance is even computed.
For example, if case differences should not count, normalize first:
If adjacent swaps should count as one operation, standard Levenshtein is not enough and Damerau-Levenshtein is closer to the intended model.
Common Pitfalls
A common mistake is using mismatch counting when strings have different lengths. That only solves the replacement-only equal-length case.
Another mistake is solving a simple replacement problem with full dynamic programming. The answer is correct, but the solution is slower and less clear than necessary.
It is also easy to forget edge cases such as empty strings, identical inputs, or normalization requirements for case and whitespace.
Summary
- The right algorithm depends entirely on which edits are allowed.
- For equal-length replacement-only problems, count mismatched positions.
- For insertion, deletion, and substitution, use Levenshtein distance.
- Space-optimized dynamic programming is useful for long strings.
- Define the rules first, or you may solve the wrong problem correctly.

