fork download
  1. arr = list(map(str, input().split()))
  2. a = arr[0]
  3. b = arr[1]
  4. # Declaring array 'D' with rows = len(a) + 1 and columns = len(b) + 1:
  5. D = [[0 for i in range(len(b) + 1)] for j in range(len(a) + 1)]
  6.  
  7. # Initialising first row:
  8. for i in range(len(a) + 1):
  9. D[i][0] = i
  10.  
  11. # Initialising first column:
  12. for j in range(len(b) + 1):
  13. D[0][j] = j
  14.  
  15. for i in range(1, len(a) + 1):
  16. for j in range(1, len(b) + 1):
  17. if a[i - 1] == b[j - 1]:
  18. D[i][j] = D[i - 1][j - 1]
  19. else:
  20. # Adding 1 to account for the cost of operation
  21. insertion = 1 + D[i][j - 1]
  22. deletion = 1 + D[i - 1][j]
  23. replacement = 1 + D[i - 1][j - 1]
  24.  
  25. # Choosing the best option:
  26. D[i][j] = min(insertion, deletion, replacement)
  27.  
  28. print(D[len(a)][len(b)])
Success #stdin #stdout 0.03s 9576KB
stdin
sunday saturday
stdout
3