easy +10 pts

Hamming Distance

Count the positions where two equal-length strings differ.

In information theory, the Hamming distance between two strings of equal length is the number of positions at which the corresponding characters are different. Write a function `hamming_distance(a: str, b: str) -> int` that returns the Hamming distance between two strings `a` and `b`. - If the strings have different lengths, raise a `ValueError` with the message `'strings must have equal length'`. - The comparison is case-sensitive: `'A'` and `'a'` are different.

Constraints

- `0 <= len(a) == len(b) <= 10^5` - Characters are ASCII printable. - Time complexity: O(n) where n is the length of the strings. - Space complexity: O(1) additional space.

Example

```python
>>> hamming_distance('toned', 'roses')
3
>>> hamming_distance('1011101', '1001001')
2
>>> hamming_distance('abc', 'abc')
0
>>> hamming_distance('', '')
0
>>> hamming_distance('abc', 'abd')
1
```
10 points ~15 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

First check if the lengths are equal; if not, raise ValueError.
Use `zip(a, b)` to pair characters from both strings.
Count how many paired characters are different.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.