Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions Day3/Java/HammingDistance1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* @author: Nomit Pahuja
* date: 09/02/2019
*/

import java.util.Scanner;

public class HammingDistance1 {

public static int hammingDistance(String word1, String word2) {
int distance = 0;
//Checking word length
if (word1.length() != word2.length()) {
return -1;
}
//Checking if symbols are same or not
for(int i = 0; i<word1.length(); i++ ) {
if(word1.charAt(i) != word2.charAt(i)) {
distance++;
}
}
return distance;
}

public static void main (String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the first string: ");
String word1 = in.next();
System.out.print("Enter the second string: ");
String word2 = in.next();
int distance = hammingDistance(word1, word2);
if(distance == -1) {
System.out.println("Please enter strings of same length");
} else {
System.out.println("The Hamming distance between the strings " + word1 + " and " + word2 + " is " + distance);
}
System.exit(0);
}
}
29 changes: 29 additions & 0 deletions Day3/JavaScript/hammingDistance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* @author Nomit Pahuja
* date: 09/02/2018
*/

function hamming (str1, str2) {
let distance = 0;

//Check the length of strings
if (str1.length !== str2.length) {
console.log("Please enter strings of same length.");
return -1;
}

//Find the hamming distance
for (let i = 0; i < str1.length ; i++) {
if(str1[i] !== str2[i]) {
distance++;
}
}

//Print the hamming distance
console.log(`The hamming distance between the strings : ${str1} and ${str2} is ${distance}`);
return distance;
}

hamming("strawberry","avocado");
hamming("john","taylor");
hamming("issac","rahul");
69 changes: 69 additions & 0 deletions Day3/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,36 @@ hammingDistance('1011101', '1001001');
hammingDistance('2173896', '2233796');
```

### [Solution] (./JavaScript/hamingDistance.js)

```js
function hamming (str1, str2) {
let distance = 0;

//Check the length of strings
if (str1.length !== str2.length) {
console.log("Please enter strings of same length.");
return -1;
}

//Find the hamming distance
for (let i = 0; i < str1.length ; i++) {
if(str1[i] !== str2[i]) {
distance++;
}
}

//Print the hamming distance
console.log(`The hamming distance between the strings : ${str1} and ${str2} is ${distance}`);
return distance;
}

hamming("strawberry","avocado");
hamming("john","taylor");
hamming("issac","rahul");
```


## Java Implementation

### [Solution](./Java/HammingDistance.java)
Expand Down Expand Up @@ -97,6 +127,45 @@ public class HammingDistance {
}
```

### [Solution] (./Java/HammingDistance1.java)

```java
import java.util.Scanner;

public class HammingDistance1 {

public static int hammingDistance(String word1, String word2) {
int distance = 0;
//Checking word length
if (word1.length() != word2.length()) {
return -1;
}
//Checking if symbols are same or not
for(int i = 0; i<word1.length(); i++ ) {
if(word1.charAt(i) != word2.charAt(i)) {
distance++;
}
}
return distance;
}

public static void main (String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the first string: ");
String word1 = in.next();
System.out.print("Enter the second string: ");
String word2 = in.next();
int distance = hammingDistance(word1, word2);
if(distance == -1) {
System.out.println("Please enter strings of same length");
} else {
System.out.println("The Hamming distance between the strings " + word1 + " and " + word2 + " is " + distance);
}
System.exit(0);
}
}
```

## C++ Implementation

### [hamming.cpp](./C++/hamming.cpp)
Expand Down