java
54 lines · 8 steps
Two-row Levenshtein distance in Java
Compute edit distance between two strings using two rolling rows instead of a full matrix.
Explained by
highlit
1public final class Levenshtein {
2
3 private Levenshtein() {
4 }
5
6 public static int distance(CharSequence a, CharSequence b) {
7 if (a.equals(b)) {
8 return 0;
9 }
10 if (a.length() == 0) {
11 return b.length();
12 }
13 if (b.length() == 0) {
14 return a.length();
15 }
16
17 int[] previous = new int[b.length() + 1];
18 int[] current = new int[b.length() + 1];
19
20 for (int j = 0; j <= b.length(); j++) {
21 previous[j] = j;
22 }
23
24 for (int i = 1; i <= a.length(); i++) {
25 current[0] = i;
26 char ca = a.charAt(i - 1);
27
28 for (int j = 1; j <= b.length(); j++) {
29 int cost = ca == b.charAt(j - 1) ? 0 : 1;
30 current[j] = Math.min(
31 Math.min(current[j - 1] + 1, previous[j] + 1),
32 previous[j - 1] + cost);
33 }
34
35 int[] swap = previous;
36 previous = current;
37 current = swap;
38 }
39
40 return previous[b.length()];
41 }
42
43 public static double similarity(CharSequence a, CharSequence b) {
44 int maxLen = Math.max(a.length(), b.length());
45 if (maxLen == 0) {
46 return 1.0;
47 }
48 return 1.0 - (double) distance(a, b) / maxLen;
49 }
50
51 public static boolean matches(CharSequence a, CharSequence b, int maxEdits) {
52 return distance(a, b) <= maxEdits;
53 }
54}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Levenshtein distance counts the minimum single-character inserts, deletes, and substitutions to turn one string into another.
- 2The classic DP only ever reads the current and previous rows, so two arrays replace the full matrix and cut memory to O(n).
- 3Swapping row references each iteration reuses buffers without reallocating, keeping the inner loop allocation-free.
Related explainers
java
package com.example.lb; import java.util.List; import java.util.concurrent.atomic.AtomicInteger;
A thread-safe round-robin load balancer in Java
concurrency
load-balancing
round-robin
Intermediate
9 steps
java
public interface GitHubClient { @GetExchange("/users/{username}") GitHubUser getUser(@PathVariable String username);
Declarative HTTP clients in Spring
http-client
declarative-api
proxy
Intermediate
8 steps
java
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;
Building a trie for autocomplete in Java
trie
prefix-tree
recursion
Intermediate
8 steps
java
@RestController @RequestMapping("/api/products") public class ProductSearchController {
Binding collection query params in Spring
rest-api
query-parameters
dependency-injection
Intermediate
6 steps
java
import java.util.ArrayDeque; import java.util.Deque; import java.util.Map;
Evaluating math expressions with two stacks
stacks
parsing
operator-precedence
Intermediate
9 steps
java
@Entity @Table(name = "orders") @SQLDelete(sql = "UPDATE orders SET deleted = true, deleted_at = now() WHERE id = ?") @Where(clause = "deleted = false")
Soft deletes with Hibernate in Spring
soft-delete
jpa
hibernate
Intermediate
9 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/two-row-levenshtein-distance-in-java-explained-java-1254/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.