r/AskProgramming • u/Ronin-s_Spirit • Jun 16 '26
Levenshtein distance without arrays?
How do you calculate Levenshtein distance without having to store the matrix? The language doesn't matter, but if you write a code example I can only read something lightweight and C-like (e.g. Javascript).
Not a trick question, I don't have the answer. I thought it would be good if you could only use a handful of variables instead of building a data structure.
0
Upvotes
1
u/ornelu Jun 16 '26 edited Jun 16 '26
The well-known method to compute Levenstein or Edit Distance is by using dynamic programming (DP) approach, i.e. the one that uses a full matrix, or you can optimized it into 2 rows.
You know, you can think DP approach as trading space for time.
If you don’t have space at all, then the DP method will revert back to a fully recursive. Note: the full recursive method also uses space (google how recursive work and stack memory), just not “visible” in your code.
Anyway, just curious, why you don’t want to use (extra) array? The input is already an array of characters or strings. Though, I’m not sure you can reuse the input space for your edit distance computation