15 lines c code ,15ms


  • 1
    8
    int arr[1024][1024] = {0};
    int min_(int a,int b,int c){
    	if(a<=b&&a<=c) return a;
    	if(b<=a&&b<=c) return b;
    	if(c<=a&&c<=b) return c;
    }
    int minDistance(char *word1, char *word2) {
    	int l1 = 0,l2 = 0;
    	for(l1 = 0;word1[l1];arr[l1][0] = ++l1);
    	for(l2 = 0;word2[l2];arr[0][l2] = ++l2);
    	for(int i = 1;i<=l1;i ++)
    		for(int j = 1;j<=l2;j ++)
    			arr[i][j] = min_(arr[i-1][j-1]+(word1[i-1]!=word2[j-1]),arr[i][j-1]+1,arr[i-1][j]+1);
    	return arr[l1][l2];
    }

Log in to reply
 

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