{"record":{"id":"4cab88e9b7612e3e","repo":"TheAlgorithms/Go","slug":"strings-must-have-a-same-length","errorCode":null,"errorMessage":"strings must have a same length","messagePattern":"strings must have a same length","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"strings/hamming/hammingdistance.go","lineNumber":20,"sourceCode":"This algorithm calculates the hamming distance between two equal length strings.\nThe Hamming distance between two equal-length strings of symbols is the number of positions\nat which the corresponding symbols are different:\nhttps://en.wikipedia.org/wiki/Hamming_distance\n\nNote that we didn't consider strings as an array of bytes, therefore, we didn't use the XOR operator.\nIn this case, we used a simple loop to compare each character of the strings, and if they are different,\nwe increment the hamming distance by 1.\n\nParameters: two strings to compare\nOutput: distance between both strings */\n\npackage hamming\n\nimport \"errors\"\n\nfunc Distance(str1, str2 string) (int, error) {\n\tif len(str1) != len(str2) {\n\t\treturn -1, errors.New(\"strings must have a same length\")\n\t}\n\n\thammingDistance := 0\n\tfor i := 0; i < len(str1); i++ {\n\t\tif str1[i] != str2[i] {\n\t\t\thammingDistance++\n\t\t}\n\t}\n\n\treturn hammingDistance, nil\n}\n","sourceCodeStart":2,"sourceCodeEnd":32,"githubUrl":"https://github.com/TheAlgorithms/Go/blob/5ba447ec5ff3d1213de65b92e726ee74c5d5cc19/strings/hamming/hammingdistance.go#L2-L32","documentation":"hamming.Distance computes the Hamming distance, which is only defined for two strings of equal length. When the input strings differ in length the function cannot align them, so it returns -1 and this error.","triggerScenarios":"Calling Distance(str1, str2) where len(str1) != len(str2), e.g. Distance('GATTACA', 'GATTACGA') or comparing strings of different byte lengths.","commonSituations":"Comparing DNA/rna sequences from sources with different trimming; comparing user-supplied strings without prior length normalization; accidentally comparing a string against its prefix or suffix.","solutions":["Check that both strings have the same length before calling Distance","Pad or trim the shorter string to match, if that is semantically valid for your data","Use a different metric (e.g. Levenshtein distance) when inputs may legitimately differ in length"],"exampleFix":"// before\nhamming.Distance(\"GATTACA\", \"GAT\")\n// after\nif len(a) == len(b) { d, err := hamming.Distance(a, b) } else { /* handle mismatch */ }","handlingStrategy":"validation","validationCode":"if len(str1) != len(str2) {\n    return 0, fmt.Errorf(\"length mismatch: %d vs %d\", len(str1), len(str2))\n}\nd, err := hamming.Distance(str1, str2)","typeGuard":"func sameLength(a, b string) bool { return len(a) == len(b) }","tryCatchPattern":"d, err := hamming.Distance(a, b)\nif err != nil {\n    if err.Error() == \"strings must have a same length\" {\n        // handle length mismatch\n    }\n    return err\n}","preventionTips":["Assert equal lengths at the data boundary (normalize sequences on ingest)","Prefer Levenshtein/Jaro metrics when lengths can differ","Add a unit test with mismatched-length inputs to lock the contract"],"tags":["hamming-distance","strings","validation"],"backgroundTag":"length-mismatch","analyzedSha":"5ba447ec5ff3d1213de65b92e726ee74c5d5cc19","analyzedAt":"2026-09-02T21:54:30.227Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-10T02:17:09.455Z"}