Hamming Distance code (Java)

dshrenik
dshrenik used Ask the Experts™
on
Does anyone have the code to find the hamming distance between 2 strings, in Java?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016
Commented:
Try
import java.util.List;

public class Hamming {
  private List<String> iWords;
  
  public Hamming(String fileName)
  {
    System.err.print("Reading database ... ");
    iWords = new java.util.ArrayList<String>();
    try {
      java.io.BufferedReader in =
	new java.io.BufferedReader(new java.io.FileReader(fileName));
      String line = in.readLine();
      String prev = null;
      while (line != null) {
	String word = line.toUpperCase();
	if (!word.equals(prev))
	  iWords.add(word);
	prev = word;
	line = in.readLine();
      }
      in.close();
    } catch (java.io.IOException e) {
      System.out.println(e);
    }
    System.err.println();
  }

  // assumes strings have the same length!
  private static int distance(String lhs, String rhs)
  {
    int d = 0;
    for (int i = 0; i < lhs.length(); ++i) {
      if (lhs.charAt(i) != rhs.charAt(i))
	++d;
    }
    return d;
  }
  
  public List<String> findMatches(String s, int maxDist)
  {
    List<String> m = new java.util.ArrayList<String>();
    for (String w : iWords) {
      if (!s.equals(w) && s.length() == w.length() 
	  && distance(s, w) <= maxDist) {
	m.add(w);
      }
    }
    return m;
  }
  
  public int countPairs(int maxDist)
  {
    int count = 0;
    for (int i = 0; i < iWords.size() - 1; ++i) {
      String s = iWords.get(i);
      for (int j = i+1; j < iWords.size(); ++j) {
	String s1 = iWords.get(j);
	if (s.length() == s1.length() && distance(s, s1) <= maxDist)
	  ++count;
      }
    }
    return count;
  }
  
  public static void main(String[] args)
  {
    if (args.length != 2 && args.length != 3) {
      System.err.println("Usage: java Hamming <filename> <max. dist> [ <word> ]");
      System.exit(1);
    }

    Hamming h = new Hamming(args[0]);
    int maxDist = Integer.parseInt(args[1]);

    if (args.length == 3) {
      String word = args[2];
      List<String> w = h.findMatches(word, maxDist);
      if (w.isEmpty())
	System.out.println("No words found.");
      else {
	System.out.println("I found the following words:");
	for (String s : w)
	  System.out.print(s + " ");
	System.out.println();
      }
    } else {
      int total = h.countPairs(maxDist);
      System.out.println("Total number of pairs at distance at most " + 
			 maxDist + " is " + total);
    }
  }
}

Open in new window

Author

Commented:
Is it possible to code that accepts just 2 words as arguments?
Top Expert 2016

Commented:
:)

>>Is it possible to code that accepts just 2 words as arguments?

Just call distance in that code

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial