{"record":{"id":"2867e2aba1742474","repo":"trekhleb/javascript-algorithms","slug":"strings-must-be-of-the-same-length","errorCode":null,"errorMessage":"Strings must be of the same length","messagePattern":"Strings must be of the same length","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/algorithms/string/hamming-distance/hammingDistance.js","lineNumber":8,"sourceCode":"/**\n * @param {string} a\n * @param {string} b\n * @return {number}\n */\nexport default function hammingDistance(a, b) {\n  if (a.length !== b.length) {\n    throw new Error('Strings must be of the same length');\n  }\n\n  let distance = 0;\n\n  for (let i = 0; i < a.length; i += 1) {\n    if (a[i] !== b[i]) {\n      distance += 1;\n    }\n  }\n\n  return distance;\n}\n","sourceCodeStart":1,"sourceCodeEnd":21,"githubUrl":"https://github.com/trekhleb/javascript-algorithms/blob/85293e3e2b88f4d2ce330d956b139cf628aa1e82/src/algorithms/string/hamming-distance/hammingDistance.js#L1-L21","documentation":"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.","triggerScenarios":"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.","commonSituations":"Hash comparisons across algorithms or encodings, variable-length identifiers, user-typed codes, or normalization applied asymmetrically to the two inputs.","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"],"exampleFix":"// before\nimport hammingDistance from './src/algorithms/string/hamming-distance/hammingDistance';\nconst d = hammingDistance(sha1Hex, md5Hex);\n// 40 vs 32 chars -> throws\n\n// after\nconst d = hammingDistance(sha1HexA, sha1HexB);\n// same algorithm and encoding: both 40 chars","handlingStrategy":"validation","validationCode":"if (typeof a !== 'string' || typeof b !== 'string' || a.length !== b.length) {\n  throw new TypeError('hammingDistance needs equal-length strings');\n}\nhammingDistance(a, b);","typeGuard":"const isEqualLengthStrings = (a, b) =>\n  typeof a === 'string' && typeof b === 'string' && a.length === b.length;","tryCatchPattern":"import levenshteinDistance from './src/algorithms/string/levenshtein-distance/levenshteinDistance';\n\nlet d;\ntry {\n  d = hammingDistance(a, b);\n} catch (e) {\n  if (e.message === 'Strings must be of the same length') {\n    d = levenshteinDistance(a, b); // fall back to edit distance\n  } else {\n    throw e;\n  }\n}","preventionTips":["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"],"tags":["string","hamming-distance","length-mismatch","validation"],"backgroundTag":"string-length-mismatch","analyzedSha":"85293e3e2b88f4d2ce330d956b139cf628aa1e82","analyzedAt":"2026-08-24T05:59:10.417Z","schemaVersion":2},"datasetVersion":"2026-08-24T07:17:09.176Z"}