trekhleb/javascript-algorithms · error · Error
Strings must be of the same length
Error message
Strings must be of the same length
What it means
hammingDistance(a, b) counts positions at which two equal-length strings differ; the guard rejects any length mismatch before the loop because Hamming distance is undefined otherwise. It is meant for fixed-width codes such as hex digests, barcodes and fixed-length tokens. For strings of different lengths where insertions and deletions matter, Levenshtein distance (also in this repository) is the correct measure.
Source
Thrown at src/algorithms/string/hamming-distance/hammingDistance.js:8
/**
* @param {string} a
* @param {string} b
* @return {number}
*/
export default function hammingDistance(a, b) {
if (a.length !== b.length) {
throw new Error('Strings must be of the same length');
}
let distance = 0;
for (let i = 0; i < a.length; i += 1) {
if (a[i] !== b[i]) {
distance += 1;
}
}
return distance;
}
View on GitHub (pinned to 85293e3e2b)
Solutions
- Log a.length and b.length to see which input differs
- Normalize both strings identically (same hash algorithm, same encoding, same case and trim) before comparing
- If lengths legitimately differ and you need edit distance, switch to the repo's levenshteinDistance
- For unequal but comparable fixed codes, pad the shorter string with a domain-defined filler only if your spec defines the semantics
Example fix
// before import hammingDistance from './src/algorithms/string/hamming-distance/hammingDistance'; const d = hammingDistance(sha1Hex, md5Hex); // 40 vs 32 chars -> throws // after const d = hammingDistance(sha1HexA, sha1HexB); // same algorithm and encoding: both 40 chars
Defensive patterns
Strategy: validation
Validate before calling
if (typeof a !== 'string' || typeof b !== 'string' || a.length !== b.length) {
throw new TypeError('hammingDistance needs equal-length strings');
}
hammingDistance(a, b); Type guard
const isEqualLengthStrings = (a, b) => typeof a === 'string' && typeof b === 'string' && a.length === b.length;
Try / catch
import levenshteinDistance from './src/algorithms/string/levenshtein-distance/levenshteinDistance';
let d;
try {
d = hammingDistance(a, b);
} catch (e) {
if (e.message === 'Strings must be of the same length') {
d = levenshteinDistance(a, b); // fall back to edit distance
} else {
throw e;
}
} Prevention
- Pin one hash algorithm and encoding per comparison site
- Normalize both inputs with the same trim/case helper
- Reserve hammingDistance for fixed-width codes; use levenshteinDistance otherwise
When it happens
Trigger: hammingDistance('karolin', 'kathrinn') (7 vs 8 chars); comparing digests from different hash algorithms (SHA-1 40 hex chars vs MD5 32); comparing hex vs base64 encodings of the same data; trimming or lowercasing one input but not the other.
Common situations: Hash comparisons across algorithms or encodings, variable-length identifiers, user-typed codes, or normalization applied asymmetrically to the two inputs.
Related errors
- The method supports only positive integers
- The data is empty
- Either dataSet or labels or toClassify were not set
- Items and weights must be of the same size
- Items must not be empty
AI-assisted analysis of trekhleb/javascript-algorithms@85293e3e2b (2026-08-24).
Data as JSON: /api/errors/2867e2aba1742474.
Report an issue: GitHub.