Categories

# [Coderbyte] – H Distance [Easy]

The coding challenge for today is called “H Distance”. In short, this challenge asks us to find the number of characters that are not found in both strings. However, Coderbyte does a better job of explaining the problem:

Have the function HDistance(strArr) take the array of strings stored in strArr, which will only contain two strings of equal length and return the number of characters at each position that are different between them. For example: if strArr is [“house”, “hours”] then your program should return 2. The string will always be of equal length and will only contain lowercase characters from the alphabet and numbers.

The key phrase here is equal length. The argument is always an array of two strings which are of equal length. That means, only one for loop is needed to iterate over the characters of both strings.

First step – Create a counter variable
The first step is to create a counter variable that counts the characters at each position that are different between them.

```
count = 0

```

For instance, if the strings are “house” and “hours”, the count variable is incremented twice because the last two characters of the two strings are different (house and hours).

Second step – Iterate over the strings
The next step is to loop over the first string (since both are of the same length) and compare whether the characters at the same position in both strings are the same or different.

```
for pos in range(len(strArr)):

```

Do not forget that the given argument is an array. Thus, strArr represents the first string, and strArr represents the second string.

Third step – Check each character and increment count where the condition is met
In this step, it compares the characters from the two strings that are at the same position. If they are not matching, the counter is incremented.

```if strArr[pos] != strArr[pos]:
count += 1
```

If the two strings are “house” and “hours”:

“h” from “house” is the same as “h” from “hours” -> do not increment “count”

“o” from “house” is the same as “o” from “hours” -> do not increment “count”

…..

“s” from “house” is not the same as “r” from “hours” -> increment “count”

“e” from “house” is not the same as “s” from “hours” -> increment “count”

The last step is to return the counter – number of different characters. My solution is in the top 20 (20th solution) on Coderbyte. My full solution is below:

```def HDistance(strArr):

count = 0

# they are the same length so only one loop needed
for pos in range(len(strArr)):
if strArr[pos] != strArr[pos]:
count += 1

return count
```

The repl.it link to the solution – here.

There is no official solution from Coderbyte. However, below are the top two solutions written by other users.
Top solution by EmperorDuty:

```def DifferentCases(str):
str = str.lower()
dirty_chars = '`1234567890-=~!@#\$%^&amp;amp;*()_+[]\{}|;\':",./&amp;lt;&amp;gt;?'
for char in dirty_chars:
str = str.replace(char, ' ')

return ''.join(
s.capitalize() for s in str.split(' ')
)

# code goes here
return str
```

The second best solution by jlents:

```import re
def DifferentCases(str):
return ''.join([x.capitalize() for x in re.sub('[^a-zA-Z]', ' ', str).lower().split()])
```