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

  1. Log a.length and b.length to see which input differs
  2. Normalize both strings identically (same hash algorithm, same encoding, same case and trim) before comparing
  3. If lengths legitimately differ and you need edit distance, switch to the repo's levenshteinDistance
  4. 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

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


AI-assisted analysis of trekhleb/javascript-algorithms@85293e3e2b (2026-08-24). Data as JSON: /api/errors/2867e2aba1742474. Report an issue: GitHub.