1

A light store owner has several bulb chains of different types which consist of bulbs of different colors in different order. In addition to that, he has large collection of bulbs of each colors. A bulb chain is identified by the color sequence of its bulbs. He wants to transform one type of bulb chain into another type of bulb chain by either by:

• Adding a bulb at some location. • Removing a bulb from a location. • Replacing a bulb with another bulb of different colour.

Given two color sequences of two different bulb chains, Find the minimum no. of operations required to do this transformation. (Assume each color can be represented by a character and hence, color sequence of a bulb chain can be represented as sequence of characters or a string.) Input/Output Specifications Input: • First color sequence (string A) • Second color sequence (String B)

Output: Minimum No. of operations required to transform first bulb chain into the second (integer)

Examples Input1 : "asdfgh" input2 : "sdfgh"

Output : 1

Input1 : "x" input2 : "asdf"

Output : 4

in the above given scenario, how to find the solution and what must be the first step ? i am an enthusiast and beginner in algorithm writing .

Alok Mishra
  • 926
  • 13
  • 39
  • The problem describes the edit distance (or Levenshtein distance) between two strings. Look at [Levenshtein distance](http://en.wikipedia.org/wiki/Levenshtein_distance) for more information. – Markus Jarderot Jan 16 '15 at 14:00
  • Interesting question, I once had a similar problem (replacing weights on dumbbels, but with extra addition they can be replaced only from one side); did not get an answer then... you might also try Math fora, since it might be a known math problem. – Michel Keijzers Jan 16 '15 at 14:00

1 Answers1

0

From wikipedia:

int LevenshteinDistance(string s, string t)
{
    // degenerate cases
    if (s == t) return 0;
    if (s.Length == 0) return t.Length;
    if (t.Length == 0) return s.Length;

    // create two work vectors of integer distances
    int[] v0 = new int[t.Length + 1];
    int[] v1 = new int[t.Length + 1];

    // initialize v0 (the previous row of distances)
    // this row is A[0][i]: edit distance for an empty s
    // the distance is just the number of characters to delete from t
    for (int i = 0; i < v0.Length; i++)
        v0[i] = i;

    for (int i = 0; i < s.Length; i++)
    {
        // calculate v1 (current row distances) from the previous row v0

        // first element of v1 is A[i+1][0]
        //   edit distance is delete (i+1) chars from s to match empty t
        v1[0] = i + 1;

        // use formula to fill in the rest of the row
        for (int j = 0; j < t.Length; j++)
        {
            var cost = (s[i] == t[j]) ? 0 : 1;
            v1[j + 1] = Minimum(v1[j] + 1, v0[j + 1] + 1, v0[j] + cost);
        }

        // copy v1 (current row) to v0 (previous row) for next iteration
        for (int j = 0; j < v0.Length; j++)
            v0[j] = v1[j];
    }

    return v1[t.Length];
}
Joe
  • 165
  • 2
  • thank you , it has helped me . though i was curious to know how to make selection of approaches for such problems . – Alok Mishra Jan 16 '15 at 14:39