Monday, December 22, 2014

One Edit Distance & Edit Distance

Given two strings S and T, determine if they are both one edit distance apart. 
Hint:
1. If | n – m | is greater than 1, we know immediately both are not one-edit distance apart.
2. It might help if you consider these cases separately, m == n and m ≠ n.
3. Assume that m is always ≤ n, which greatly simplifies the conditional statements. If m > n, we could just simply swap S and T.
4. If m == n, it becomes finding if there is exactly one modified operation. If m ≠ n, you do not have to consider the delete operation. Just consider the insert operation in T.
From Wikipedia:
In computer scienceedit distance is a way of quantifying how dissimilar two strings (e.g., words) are to one another by counting the minimum number of operations required to transform one string into the other.

 Separate the case that S and T have different lengths and have same lengths. Consider some corner cases.
Note since edit distance only allow Insertion, Deletion, and Substitution, the following strings are not one edit distance.

S = " fdgvf "
T = " dfgvf "


public class OneEditDistance {
 public boolean isOneEditDistance(String s, String t)
 {
  if ((s == null && t != null) || (t == null && s != null))
   return false;
  if (s == null && t == null)
   return true;
  if (s.equals(t))
   return true;
  if (Math.abs(t.length() - s.length() )> 1)
   return false;
  if (t.length() == s.length())
   return isOneEditSameLength(s, t);
  return isOneEditDiffLength(s, t);
 }
 private boolean isOneEditSameLength(String s, String t)
 {
  int diff = 0;
  for (int i = 0; i < s.length(); i++)
  {
   if(s.charAt(i) != t.charAt(i))
    diff++;
   if (diff > 1)
    return false;
  }
  return true;
 }
 private boolean isOneEditDiffLength(String s, String t)
 {
  if (s.length() > t.length())
  {
   String tmp = s;
   s = t;
   t = tmp;
  }
  int index = 0;
  while (index < s.length() && s.charAt(index) == t.charAt(index))
  {
   System.out.println(index);
   index++;
  }
  if (index == s.length())
   return true;
  return s.substring(index).equals(t.substring(index + 1));
 }
 
}

Moreover, I find it's helpful to also post this classic DP algorithm to calculate the edit distance of two strings.


public class EditDistance {
    public int minDistance(String word1, String word2) {
        if (word1 == null || word2 == null)
            throw new Error("Null string(s)");
        
        int[][] distance = new int[word1.length() + 1][word2.length() + 1];
        for (int i = 0; i < distance.length; i++)
            distance[i][0] = i;
        for (int j = 1; j < distance[0].length; j++)
            distance[0][j] = j;
        for (int i = 1; i < distance.length; i++)
        {
            for (int j = 1; j < distance[0].length; j++)
            {
                if (word1.charAt(i - 1) == word2.charAt(j - 1))
                    distance[i][j] = distance[i - 1][j - 1];
                else
                {
                    distance[i][j] = Math.min(distance[i - 1][j], distance[i][j - 1]) + 1;
                    distance[i][j] = Math.min(distance[i][j], distance[i - 1][j - 1] + 1);
                }
            }
        }
        return distance[word1.length()][word2.length()];
    }
}

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