Skip to content

Commit f66a226

Browse files
Create HammingDistance.java
1 parent da0415c commit f66a226

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
public class HammingDistance{
2+
public static vois main(String args[]){
3+
System.out.println(getHammingDistance("1011101", "1001001")); # Output 2
4+
System.out.println(getHammingDistance("Welcome", "welcome")); # Output 0
5+
System.out.println(getHammingDistance("hacking", "cryptography")) # Output "String are not of equal length."
6+
}
7+
8+
private static int getHammingDistance(String str1, String str2){
9+
int distance = 0;
10+
if (str1.length() != str2.length()) {
11+
System.out.println("String are not of equal length.");
12+
System.exit(0);
13+
} else {
14+
for (int i = 0; i < str1.length(); i++) {
15+
if (str1.toLowerCase().charAt(i) != str2.toLowerCase().charAt(i)) {
16+
distance++;
17+
}
18+
}
19+
}
20+
return distance;
21+
}
22+
}

0 commit comments

Comments
 (0)