Skip to content

Commit

Permalink
levenshtein: Zig: reinstall early termination checks
Browse files Browse the repository at this point in the history
  • Loading branch information
PEZ committed Dec 18, 2024
1 parent daccbe4 commit 4fbe62d
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions levenshtein/zig/code.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ const std = @import("std");
/// Space Complexity: O(min(m,n)) - only uses two arrays instead of full matrix
/// Time Complexity: O(m*n) where m and n are the lengths of the input strings
fn levenshteinDistance(s1: []const u8, s2: []const u8) usize {
// Early termination checks
if (std.mem.eql(u8, s1, s2)) return 0;
if (s1.len == 0) return s2.len;
if (s2.len == 0) return s1.len;

// Make s1 the shorter string for space optimization
const str1 = if (s1.len > s2.len) s2 else s1;
const str2 = if (s1.len > s2.len) s1 else s2;
Expand Down

0 comments on commit 4fbe62d

Please sign in to comment.