File tree Expand file tree Collapse file tree 1 file changed +22
-0
lines changed Expand file tree Collapse file tree 1 file changed +22
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments