Skip to content

Conversation

@AliAlimohammadi
Copy link
Contributor

Description

This PR adds an implementation of the Hamming distance algorithm to the bit manipulation module. The Hamming distance is a metric for comparing two binary data strings, measuring the minimum number of substitutions required to change one string into the other.

Implementation Details

The implementation provides two public functions:

  1. hamming_distance(a: u64, b: u64) -> u64

    • Calculates the Hamming distance between two 64-bit unsigned integers
    • Uses XOR operation to identify differing bits
    • Counts set bits in the XOR result using a helper function
  2. hamming_distance_str(a: &str, b: &str) -> u64

    • Calculates the Hamming distance between two strings of equal length
    • Iterates through character pairs and counts differences
    • Panics with a descriptive message if string lengths differ

Algorithm Approach

For integers:

  • $XOR$ the two numbers (this sets bits where they differ)
  • Count the number of set bits in the result
  • Time complexity: $O(log n)$ where n is the value of the larger number
  • Space complexity: $O(1)$

For strings:

  • Zip the two strings together
  • Filter and count positions where characters differ
  • Time complexity: $O(n)$ where n is the string length
  • Space complexity: $O(1)$

Changes Made

  • ✅ Added src/bit_manipulation/hamming_distance.rs with complete implementation
  • ✅ Updated src/bit_manipulation/mod.rs to include and export the new module
  • ✅ Implemented comprehensive unit tests covering:
    • Integer Hamming distance with multiple test cases
    • String Hamming distance with various scenarios
    • Panic behavior for strings of different lengths
    • Edge cases (identical values, zero values)
  • ✅ Added documentation with examples for all public functions
  • ✅ Included doctests that demonstrate usage

Type of Change

  • New algorithm implementation
  • Documentation (docstrings and examples)
  • Tests

Testing

All tests pass successfully:

cargo test hamming_distance
cargo test --doc
cargo fmt --check
cargo clippy -- -D warnings

Test Coverage

  • ✅ Basic integer Hamming distance calculations
  • ✅ Edge cases (zero, identical values)
  • ✅ String Hamming distance with binary strings
  • ✅ String Hamming distance with alphanumeric strings
  • ✅ Panic behavior for mismatched string lengths
  • ✅ Doctests for public API

Examples

use the_algorithms_rust::bit_manipulation::{hamming_distance, hamming_distance_str};

// Integer example: 11 (1011) vs 2 (0010) = 2 differing bits
assert_eq!(hamming_distance(11, 2), 2);

// String example
assert_eq!(hamming_distance_str("1101", "1111"), 1);
assert_eq!(hamming_distance_str("alpha", "alphb"), 1);

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my implementation is correct
  • New and existing unit tests pass locally with my changes
  • I have checked my code with cargo fmt
  • I have checked my code with cargo clippy
  • Any dependent changes have been merged and published

@AliAlimohammadi
Copy link
Contributor Author

@siriak, this is ready to be merged.

@codecov-commenter
Copy link

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.78%. Comparing base (ba7ca98) to head (4caa180).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #983      +/-   ##
==========================================
+ Coverage   95.77%   95.78%   +0.01%     
==========================================
  Files         353      354       +1     
  Lines       23363    23407      +44     
==========================================
+ Hits        22376    22421      +45     
+ Misses        987      986       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@siriak siriak merged commit 35a5743 into TheAlgorithms:master Dec 30, 2025
7 checks passed
@AliAlimohammadi AliAlimohammadi deleted the add-hamming-distance branch December 30, 2025 22:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants