-
-
Notifications
You must be signed in to change notification settings - Fork 486
Expand file tree
/
Copy pathhamming_distance.dart
More file actions
38 lines (33 loc) · 991 Bytes
/
hamming_distance.dart
File metadata and controls
38 lines (33 loc) · 991 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// The Hamming distance between two strings of equal length is
// the number of positions at which the corresponding symbols are different.
// https://en.wikipedia.org/wiki/Hamming_distance
int hamming_distance(String stringA, String stringB) {
//Calculates Hamming Distance
int distance;
//strings must be of equal length
if (stringA.length != stringB.length) {
print('String lengths must be same!');
return 0;
} else {
distance = 0;
for (var i = 0; i < stringA.length; i++) {
if (stringA[i] != stringB[i]) {
distance += 1;
}
}
}
return distance;
}
void main() {
String stringA;
String stringB;
int dist;
stringA = 'karolin';
stringB = 'kathrin';
dist = hamming_distance(stringA, stringB);
print('Hamming Distance between $stringA and $stringB is $dist');
stringA = '1011101';
stringB = '1001001';
dist = hamming_distance(stringA, stringB);
print('Hamming Distance between $stringA and $stringB is $dist');
}