Add hamming distance (#3270)

This commit is contained in:
Mihir Sawant
2022-09-24 18:44:39 +05:30
committed by GitHub
parent 07a5531f1a
commit 2fbb1d6402
2 changed files with 114 additions and 0 deletions

View File

@ -0,0 +1,38 @@
package com.thealgorithms.others.cn;
import java.util.ArrayList;
import java.util.List;
public class HammingDistance {
public int getHammingDistanceBetweenBits(String senderBits, String receiverBits) {
if(senderBits.length() != receiverBits.length()) {
throw new IllegalArgumentException("Sender and Receiver bits should be same");
}
List<byte[]> byteArray = new ArrayList<>();
byteArray.add(senderBits.getBytes());
byteArray.add(receiverBits.getBytes());
byte[] senderData = byteArray.get(0);
byte[] receiverData = byteArray.get(1);
int totalErrorBitCount = 0;
for(int i = 0; i < senderData.length; i++){
totalErrorBitCount += senderData[i] ^ receiverData[i];
}
if(totalErrorBitCount == 0){
System.out.println("No Error bit in data segments");
} else{
System.out.println("Total Error bit count "+totalErrorBitCount);
}
return totalErrorBitCount;
}
}