rishiilluri

Untitled

Oct 7th, 2022
864
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.76 KB | None | 0 0
  1. def edit_distance(string1 , string2):
  2.     n = len(string1)
  3.     m = len(string2)
  4.     dp = []
  5.     for i in range(n+1):
  6.         temp = []
  7.         for j in range(m+1):
  8.             temp.append(0)
  9.         dp.append(temp)
  10.    
  11.     for i in range(n+1):
  12.         for j in range(m+1):
  13.             if i == 0:
  14.                 dp[i][j] = j
  15.             elif j == 0:
  16.                 dp[i][j] = i
  17.             elif string1[i-1] == string2[j-1]:
  18.                 dp[i][j] = dp[i-1][j-1]
  19.             else:
  20.                 dp[i][j] = min(dp[i-1][j-1]+1, dp[i][j-1]+1, dp[i-1][j]+1)
  21.                
  22.     return dp[n][m]
  23.            
  24.            
  25.  
  26. print(edit_distance("dussehra", "dsusehar"))
  27. print(edit_distance("ihtiha", "ihtas"))
  28. print(edit_distance("cheeks", "checks"))
Advertisement
Add Comment
Please, Sign In to add comment