Clean c++ solution


  • 0

    Similar to wildcard matching.

    int minDistance(string word1, string word2) {
        int size1 = word1.size(), size2 = word2.size();
        vector<int> steps(size2 + 1);
        iota(steps.begin(), steps.end(), 0);
        for (int i = 0; i < size1; i++) {
            int prev = steps[0];
            steps[0] = i + 1;
            for (int j = 0; j < size2; j++) {
                int temp = steps[j + 1];
                if (word1[i] == word2[j]) {
                    steps[j + 1] = prev;
                } else
                    steps[j + 1] = min(prev, min(steps[j + 1], steps[j])) + 1;
                prev = temp;
            }
        }
        return steps[size2];
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.