Saturday, December 27, 2014

Find minimum distance between words

/* This class will be given a list of words (such as might be tokenized
 * from a paragraph of text), and will provide a method that takes two
 * words and returns the shortest distance (in words) between those two
 * words in the provided text. 
 * Example:
 *   WordDistanceFinder finder = new WordDistanceFinder(Arrays.asList("the", "quick", "brown", "fox", "quick"));
 *   assert(finder.distance("fox","the") == 3);
 *   assert(finder.distance("quick", "fox") == 1);
 * /

Click here for the original question.
O(n) complexity and O(1) space complexity.




public class DistanceFinder {
 public int WordDistance(String[] words, String word1, String word2) {
  if (words == null || word1 == null || word2 == null) 
   throw new NullPointerException("Null array or words");
  if (words.length == 0)
   return Integer.MAX_VALUE;
  if (word1.equals(word2))
   return 0;
  int index1 = -1;
  int index2 = -1;
  int minDistance = Integer.MAX_VALUE;
  for (int i = 0; i < words.length; i++) {
   if (words[i] == word1) {
    index1 = i;
   }
   if (words[i] == word2) {
    index2 = i;
   }
   if ((words[i] == word1 || words[i] == word2) && (index1 != -1 && index2 != -1)) {
    minDistance = Math.min(minDistance, Math.abs(index1 - index2));
   }
  }
  if (index1 == -1 && index2 == -1)
   throw new IllegalArgumentException ("Cannot find the word!");
  return minDistance; 
 }

}

1 comment:

  1. The development of artificial intelligence (AI) has propelled more programming architects, information scientists, and different experts to investigate the plausibility of a vocation in machine learning. Notwithstanding, a few newcomers will in general spotlight a lot on hypothesis and insufficient on commonsense application. IEEE final year projects on machine learning In case you will succeed, you have to begin building machine learning projects in the near future.

    Projects assist you with improving your applied ML skills rapidly while allowing you to investigate an intriguing point. Furthermore, you can include projects into your portfolio, making it simpler to get a vocation, discover cool profession openings, and Final Year Project Centers in Chennai even arrange a more significant compensation.


    Data analytics is the study of dissecting crude data so as to make decisions about that data. Data analytics advances and procedures are generally utilized in business ventures to empower associations to settle on progressively Python Training in Chennai educated business choices. In the present worldwide commercial center, it isn't sufficient to assemble data and do the math; you should realize how to apply that data to genuine situations such that will affect conduct. In the program you will initially gain proficiency with the specialized skills, including R and Python dialects most usually utilized in data analytics programming and usage; Python Training in Chennai at that point center around the commonsense application, in view of genuine business issues in a scope of industry segments, for example, wellbeing, promoting and account.

    ReplyDelete