Hướng dẫn weighted edit distance python - Python chỉnh sửa khoảng cách có trọng số

Tôi đang tìm hiểu về khoảng cách chỉnh sửa lần đầu tiên và chỉ được mã hóa trong một vài tháng. Tôi đang cố gắng sửa đổi thuật toán sao cho các hoạt động chỉnh sửa khác nhau mang các trọng số khác nhau như sau: chèn nặng 20, xóa nặng 20 và thay thế nặng 5.

Tôi đã có thể thực hiện mã cơ bản tính toán khoảng cách chỉnh sửa tối thiểu nếu tất cả các hoạt động có trọng lượng bằng nhau (khoảng cách Levenshtein). Nhưng làm thế nào một người sẽ thực hiện nó nếu chúng khác nhau như đã nêu ở trên? Đây là những gì tôi có vào lúc này:

str1="algorithms"
str2="alligator"
m=len(str1)
n=len(str2)

def editdistance(str1, str2, m, n):
  table=[[0 for x in range(n+1)] for x in range(m+1)]
  
  for i in range(m+1):
    for j in range(n+1):

      if i==0:
        table[i][j]=j

      elif j==0:
        table[i][j]=i

      elif str1[i-1]==str2[j-1]:
        table[i][j]=table[i-1][j-1]

      else:
         table[i][j] = min(20+table[i][j-1], 20+table[i-1][j], 5+table[i-1][j-1])
        

  return table[m][n]

print(editdistance(str1, str2, m, n)) 

Đầu ra là 46, rõ ràng là sai vì câu trả lời phải là bội số của 5. Tôi còn thiếu gì ở đây? Mọi sự trợ giúp sẽ rất được trân trọng.

Cho hai chuỗi Str1 và Str2 trở xuống các hoạt động có thể được thực hiện trên STR1. Tìm số lượng chỉnh sửa tối thiểu (hoạt động) cần thiết để chuyển đổi ‘str1, thành‘ str2, & nbsp; & nbsp;

  1. Chèn
  2. Loại bỏ
  3. Thay thế

Tất cả các hoạt động trên có chi phí bằng nhau. & NBSP;

Examples: 

Đầu vào: & nbsp; & nbsp; str1 = Hồi Geek, str2 = đầu ra của Gesek Gesek: & nbsp;  str1 = “geek”, str2 = “gesek”
Output:  1
Explanation: We can convert str1 into str2 by inserting a ‘s’.

Đầu vào: & nbsp; str1 = người Cat Cat, str2 = đầu ra cắt giảm: & nbsp; 1Explanation: Chúng ta có thể chuyển đổi str1 thành str2 bằng cách thay thế ’a, bằng’ u.   str1 = “cat”, str2 = “cut”
Output:  1
Explanation: We can convert str1 into str2 by replacing ‘a’ with ‘u’.

Đầu vào: & nbsp; & nbsp; str1 = Hồi Chủ nhật, str2 = đầu ra thứ bảy của Saturday: & nbsp; 3Explanation: ba nhân vật cuối cùng và đầu tiên giống nhau. & nbsp; về cơ bản, chúng tôi cần phải chuyển đổi các ứng dụng của UN. & nbsp; Điều này có thể được thực hiện bằng cách sử dụng dưới ba hoạt động. Thay thế ‘n, bằng‘ r, chèn t, chèn một  str1 = “sunday”, str2 = “saturday”
Output:  3
Explanation: Last three and first characters are same.  We basically need to convert “un” to “atur”.  This can be done using below three operations. Replace ‘n’ with ‘r’, insert t, insert a

Các biểu tượng con trong trường hợp này là gì? đang đi qua. & nbsp; & nbsp; 
The idea is to process all characters one by one starting from either from left or right sides of both strings. 
Let us traverse from right corner, there are two possibilities for every pair of character being traversed.  

m: Length of str1 (first string)
n: Length of str2 (second string)
  1. Nếu các ký tự cuối cùng của hai chuỗi là giống nhau, không có gì nhiều để làm. Bỏ qua các ký tự cuối cùng và nhận được số lượng cho các chuỗi còn lại. Vì vậy, chúng tôi tái diễn cho độ dài M-1 và N-1.
  2. Khác (nếu các ký tự cuối cùng không giống nhau), chúng tôi xem xét tất cả các hoạt động trên ‘str1, hãy xem xét cả ba hoạt động trên ký tự cuối cùng của chuỗi đầu tiên, tính toán chi phí tối thiểu cho cả ba hoạt động và mất tối thiểu ba giá trị. & Nbsp;
    1. Chèn: Tái phát cho M và N-1
    2. Xóa: Tái phát cho M-1 và N
    3. Thay thế: Tái phát cho M-1 và N-1

Dưới đây là thực hiện giải pháp đệ quy ngây thơ ở trên.

C++

#include

using namespace std;

int min(int x, int

m: Length of str1 (first string)
n: Length of str2 (second string)
1int
m: Length of str1 (first string)
n: Length of str2 (second string)
3
m: Length of str1 (first string)
n: Length of str2 (second string)
4
m: Length of str1 (first string)
n: Length of str2 (second string)
5

int

m: Length of str1 (first string)
n: Length of str2 (second string)
7int
m: Length of str1 (first string)
n: Length of str2 (second string)
9int #include 1

#include 2

#include 3#include 4 #include 5

#include 6

m: Length of str1 (first string)
n: Length of str2 (second string)
4 #include 8

#include 3#include 4 using1

#include 6

m: Length of str1 (first string)
n: Length of str2 (second string)
4 using4

#include 3#include 4 using7

#include 6

m: Length of str1 (first string)
n: Length of str2 (second string)
4 namespace0

#include 3

m: Length of str1 (first string)
n: Length of str2 (second string)
4 namespace3

namespace4namespace5

namespace6namespace7

namespace6namespace9

std;0std;1

std;2std;3

std;4

int std;6

#include 2

#include 3std;9int0int1

#include 3int3int4int1

#include 3int7

int8int9

#include 3

m: Length of str1 (first string)
n: Length of str2 (second string)
4 min(2

std;4

Java

min(4 min(5

#include 3min(7 int min(int x, int

m: Length of str1 (first string)
n: Length of str2 (second string)
1int int5

#include 3#include 2

#include 6#include 4 x, 0

x, 1

m: Length of str1 (first string)
n: Length of str2 (second string)
4 x, 3

#include 6#include 4 x, 6

x, 1

m: Length of str1 (first string)
n: Length of str2 (second string)
4 x, 9

#include 6

m: Length of str1 (first string)
n: Length of str2 (second string)
01

x, 1

m: Length of str1 (first string)
n: Length of str2 (second string)
4
m: Length of str1 (first string)
n: Length of str2 (second string)
04

#include 3std;4

#include 3min(7 int

m: Length of str1 (first string)
n: Length of str2 (second string)
10int
m: Length of str1 (first string)
n: Length of str2 (second string)
12

m: Length of str1 (first string)
n: Length of str2 (second string)
13int #include 1

#include 3#include 2

#include 6#include 4

m: Length of str1 (first string)
n: Length of str2 (second string)
20
m: Length of str1 (first string)
n: Length of str2 (second string)
21
m: Length of str1 (first string)
n: Length of str2 (second string)
22

x, 1

m: Length of str1 (first string)
n: Length of str2 (second string)
4 #include 8

#include 6#include 4

m: Length of str1 (first string)
n: Length of str2 (second string)
28
m: Length of str1 (first string)
n: Length of str2 (second string)
21
m: Length of str1 (first string)
n: Length of str2 (second string)
22

x, 1

m: Length of str1 (first string)
n: Length of str2 (second string)
4 using4

#include 6#include 4

m: Length of str1 (first string)
n: Length of str2 (second string)
36namespace3
m: Length of str1 (first string)
n: Length of str2 (second string)
38namespace3
m: Length of str1 (first string)
n: Length of str2 (second string)
40

x, 1

m: Length of str1 (first string)
n: Length of str2 (second string)
4
m: Length of str1 (first string)
n: Length of str2 (second string)
43namespace3
m: Length of str1 (first string)
n: Length of str2 (second string)
45namespace3std;3

#include 6

m: Length of str1 (first string)
n: Length of str2 (second string)
4 namespace3

x, 1

m: Length of str1 (first string)
n: Length of str2 (second string)
52namespace3
m: Length of str1 (first string)
n: Length of str2 (second string)
54

m: Length of str1 (first string)
n: Length of str2 (second string)
55
m: Length of str1 (first string)
n: Length of str2 (second string)
43namespace3
m: Length of str1 (first string)
n: Length of str2 (second string)
58

m: Length of str1 (first string)
n: Length of str2 (second string)
55
m: Length of str1 (first string)
n: Length of str2 (second string)
43namespace3
m: Length of str1 (first string)
n: Length of str2 (second string)
62

m: Length of str1 (first string)
n: Length of str2 (second string)
63
m: Length of str1 (first string)
n: Length of str2 (second string)
64namespace3
m: Length of str1 (first string)
n: Length of str2 (second string)
66

m: Length of str1 (first string)
n: Length of str2 (second string)
67std;3

#include 3std;4

#include 3

m: Length of str1 (first string)
n: Length of str2 (second string)
72 min(7
m: Length of str1 (first string)
n: Length of str2 (second string)
74
m: Length of str1 (first string)
n: Length of str2 (second string)
75

#include 3#include 2

#include 6

m: Length of str1 (first string)
n: Length of str2 (second string)
79int0int1

#include 6

m: Length of str1 (first string)
n: Length of str2 (second string)
83int4int1

#include 6

m: Length of str1 (first string)
n: Length of str2 (second string)
87

x, 1

m: Length of str1 (first string)
n: Length of str2 (second string)
89

#include 3std;4

std;4

Python3

m: Length of str1 (first string)
n: Length of str2 (second string)
93
m: Length of str1 (first string)
n: Length of str2 (second string)
94

#include 3#include 4

m: Length of str1 (first string)
n: Length of str2 (second string)
97
m: Length of str1 (first string)
n: Length of str2 (second string)
98
m: Length of str1 (first string)
n: Length of str2 (second string)
98
m: Length of str1 (first string)
n: Length of str2 (second string)
21#include 01

#include 6

m: Length of str1 (first string)
n: Length of str2 (second string)
4 #include 04

#include 3#include 4 #include 07

m: Length of str1 (first string)
n: Length of str2 (second string)
98
m: Length of str1 (first string)
n: Length of str2 (second string)
98
m: Length of str1 (first string)
n: Length of str2 (second string)
21#include 01

#include 6

m: Length of str1 (first string)
n: Length of str2 (second string)
4 #include 14

#include 3#include 4 #include 17#include 18namespace3__

#include 6

m: Length of str1 (first string)
n: Length of str2 (second string)
4 #include 29#include 18namespace3#include 32#include 18namespace3
m: Length of str1 (first string)
n: Length of str2 (second string)
22

#include 3

m: Length of str1 (first string)
n: Length of str2 (second string)
4 namespace3 #include 39 #include 40#include 41__

#include 45#include 29#include 18namespace3#include 49

#include 45#include 29#include 18namespace3#include 32#include 18namespace3#include 57

#include 45

m: Length of str1 (first string)
n: Length of str2 (second string)
22

#include 60

m: Length of str1 (first string)
n: Length of str2 (second string)
98 int0

#include 63

m: Length of str1 (first string)
n: Length of str2 (second string)
98 int4

#include 66 #include 67#include 68#include 69#include 68#include 71

C#

using #include 73

min(4 #include 75

#include 3min(7 int min(int x, int

m: Length of str1 (first string)
n: Length of str2 (second string)
1int int5

#include 3#include 2

#include 6#include 4 x, 0

x, 1

m: Length of str1 (first string)
n: Length of str2 (second string)
4 x, 3

#include 6#include 4 x, 6

x, 1

m: Length of str1 (first string)
n: Length of str2 (second string)
4 x, 9

#include 6

m: Length of str1 (first string)
n: Length of str2 (second string)
01

x, 1

m: Length of str1 (first string)
n: Length of str2 (second string)
4
m: Length of str1 (first string)
n: Length of str2 (second string)
04

#include 3std;4

#include 3min(7 int

m: Length of str1 (first string)
n: Length of str2 (second string)
10int
m: Length of str1 (first string)
n: Length of str2 (second string)
12

m: Length of str1 (first string)
n: Length of str2 (second string)
13int #include 1

#include 3#include 2

#include 6#include 4 x, 0

x, 1

m: Length of str1 (first string)
n: Length of str2 (second string)
4 #include 8

#include 6#include 4

m: Length of str1 (first string)
n: Length of str2 (second string)
28
m: Length of str1 (first string)
n: Length of str2 (second string)
21
m: Length of str1 (first string)
n: Length of str2 (second string)
22

x, 1

m: Length of str1 (first string)
n: Length of str2 (second string)
4 using4

#include 6#include 4

m: Length of str1 (first string)
n: Length of str2 (second string)
36namespace3
m: Length of str1 (first string)
n: Length of str2 (second string)
38namespace3
m: Length of str1 (first string)
n: Length of str2 (second string)
40

x, 1

m: Length of str1 (first string)
n: Length of str2 (second string)
4
m: Length of str1 (first string)
n: Length of str2 (second string)
43namespace3
m: Length of str1 (first string)
n: Length of str2 (second string)
45namespace3std;3

#include 6

m: Length of str1 (first string)
n: Length of str2 (second string)
4 namespace3

x, 1namespace5

m: Length of str1 (first string)
n: Length of str2 (second string)
55namespace7

m: Length of str1 (first string)
n: Length of str2 (second string)
55namespace9

m: Length of str1 (first string)
n: Length of str2 (second string)
63std;1

m: Length of str1 (first string)
n: Length of str2 (second string)
67std;3

#include 3std;4

#include 3

m: Length of str1 (first string)
n: Length of str2 (second string)
72 min(7
m: Length of str1 (first string)
n: Length of str2 (second string)
74
m: Length of str1 (first string)
n: Length of str2 (second string)
75

#include 3#include 2

#include 6

m: Length of str1 (first string)
n: Length of str2 (second string)
79int0int1

#include 6

m: Length of str1 (first string)
n: Length of str2 (second string)
87

#include 6using67

x, 1using69

#include 3std;4

std;4

m: Length of str1 (first string) n: Length of str2 (second string)93 m: Length of str1 (first string) n: Length of str2 (second string)94

using73

#include 3#include 4

m: Length of str1 (first string)
n: Length of str2 (second string)
97
m: Length of str1 (first string)
n: Length of str2 (second string)
98
m: Length of str1 (first string)
n: Length of str2 (second string)
98
m: Length of str1 (first string)
n: Length of str2 (second string)
21#include 01

using80using81using77using83

m: Length of str1 (first string)
n: Length of str2 (second string)
22

#include 2

#include 6

m: Length of str1 (first string)
n: Length of str2 (second string)
4 #include 04

#include 3#include 4 #include 07

m: Length of str1 (first string)
n: Length of str2 (second string)
98
m: Length of str1 (first string)
n: Length of str2 (second string)
98
m: Length of str1 (first string)
n: Length of str2 (second string)
21#include 01

#include 6

m: Length of str1 (first string)
n: Length of str2 (second string)
4 #include 14

#include 3#include 4 #include 17#include 18namespace3__

#include 6

m: Length of str1 (first string)
n: Length of str2 (second string)
4 #include 29#include 18namespace3#include 32#include 18namespace3
m: Length of str1 (first string)
n: Length of str2 (second string)
22

#include 3#include 2

#include 6

m: Length of str1 (first string)
n: Length of str2 (second string)
4 using75using76using777____378
m: Length of str1 (first string)
n: Length of str2 (second string)
62

namespace24using81 namespace26using83 namespace28

#include 3std;4

#include 3

m: Length of str1 (first string)
n: Length of str2 (second string)
4 namespace33using76using777____378
m: Length of str1 (first string)
n: Length of str2 (second string)
62

namespace38using81using77 ____383 namespace42

#include 45using75using76using77using78

m: Length of str1 (first string)
n: Length of str2 (second string)
62

namespace38using81 namespace26using83

m: Length of str1 (first string)
n: Length of str2 (second string)
54

#include 45using75using76using77using78

m: Length of str1 (first string)
n: Length of str2 (second string)
62

namespace38using81 namespace26using83 namespace64

std;4

using76 namespace67int0int1

using78 namespace67int4int1

namespace74 using75using76using77 ____378using777____480using88using76namespace83

namespace38namespace80using88using78namespace88

namespace89

JavaScript

namespace90

using74 namespace92

#include 2

#include 3#include 4 x, 0

#include 6

m: Length of str1 (first string)
n: Length of str2 (second string)
4 x, 3

#include 3#include 4 x, 6

#include 6

m: Length of str1 (first string)
n: Length of str2 (second string)
4 x, 9

#include 3

m: Length of str1 (first string)
n: Length of str2 (second string)
01

#include 6

m: Length of str1 (first string)
n: Length of str2 (second string)
4
m: Length of str1 (first string)
n: Length of str2 (second string)
04

std;4

using74 std;13

#include 2

#include 3#include 4 #include 5

#include 6

m: Length of str1 (first string)
n: Length of str2 (second string)
4 #include 8

#include 3#include 4 using1

#include 6

m: Length of str1 (first string)
n: Length of str2 (second string)
4 using4

#include 3#include 4 using7

#include 6

m: Length of str1 (first string)
n: Length of str2 (second string)
4 namespace0

#include 3

m: Length of str1 (first string)
n: Length of str2 (second string)
4 std;35

#include 3std;37

#include 6namespace7

#include 6std;41

std;4

std;43int0int1

std;46int4int1

std;49

std;50std;51

std;52

Độ phức tạp thời gian của giải pháp trên là theo cấp số nhân. Trong trường hợp xấu nhất, cuối cùng chúng ta có thể thực hiện các hoạt động O (3M). Trường hợp xấu nhất xảy ra khi không có ký tự nào của hai chuỗi phù hợp. Dưới đây là sơ đồ cuộc gọi đệ quy cho trường hợp xấu nhất. & NBSP; Không gian phụ trợ: O (1), vì không có thêm không gian được sử dụng.time complexity of above solution is exponential. In worst case, we may end up doing O(3m) operations. The worst case happens when none of characters of two strings match. Below is a recursive call diagram for worst case. 
Auxiliary Space: O(1), because no extra space is utilized.

Hướng dẫn weighted edit distance python - Python chỉnh sửa khoảng cách có trọng số

Chúng ta có thể thấy rằng nhiều vấn đề phụ được giải quyết, ví dụ, ví dụ, ed (2, 2) được gọi là ba lần. Vì các vấn đề nhỏ được gọi là lại, vấn đề này có thuộc tính SubProplips chồng chéo. Vì vậy, chỉnh sửa vấn đề khoảng cách có cả hai thuộc tính (xem điều này và điều này) của một vấn đề lập trình động. Giống như các vấn đề lập trình động (DP) điển hình khác, các phép tính lại của cùng một biểu tượng con có thể tránh được bằng cách xây dựng một mảng tạm thời lưu trữ kết quả của các vấn đề phụ.

C++

#include

using namespace std;

int min(int x, int

m: Length of str1 (first string)
n: Length of str2 (second string)
1int
m: Length of str1 (first string)
n: Length of str2 (second string)
3
m: Length of str1 (first string)
n: Length of str2 (second string)
4
m: Length of str1 (first string)
n: Length of str2 (second string)
5

int std;68int

m: Length of str1 (first string)
n: Length of str2 (second string)
9int #include 1

#include 2

#include 3int std;76

#include 3std;78 using88int std;81

#include 6std;78 using88int std;86

x, 1#include 4 std;89

std;90std;91

x, 1

m: Length of str1 (first string)
n: Length of str2 (second string)
01 #include 4 std;95

std;90std;97

x, 1

m: Length of str1 (first string)
n: Length of str2 (second string)
01 #include 4 int01

std;90int03

x, 1

m: Length of str1 (first string)
n: Length of str2 (second string)
01

std;90int07

int08int09

using80int11

namespace24int13

namespace24int15

#include 6std;4

#include 3std;4

#include 3

m: Length of str1 (first string)
n: Length of str2 (second string)
4 int22

std;4

int std;6

#include 2

#include 3std;9int0int1

#include 3int3int4int1

#include 3int36

int37int9

#include 3

m: Length of str1 (first string)
n: Length of str2 (second string)
4 min(2

std;4

Java

min(4 min(5

#include 3min(7 int min(int x, int

m: Length of str1 (first string)
n: Length of str2 (second string)
1int int5

#include 3#include 2

#include 6#include 4 x, 0

x, 1

m: Length of str1 (first string)
n: Length of str2 (second string)
4 x, 3

#include 6#include 4 x, 6

x, 1

m: Length of str1 (first string)
n: Length of str2 (second string)
4 x, 9

#include 6

m: Length of str1 (first string)
n: Length of str2 (second string)
01

x, 1

m: Length of str1 (first string)
n: Length of str2 (second string)
4
m: Length of str1 (first string)
n: Length of str2 (second string)
04

#include 3std;4

#include 3min(7 int int79int

m: Length of str1 (first string)
n: Length of str2 (second string)
12

std;0____6 #include 1

#include 3#include 2

#include 6int int89int90 int__692

#include 6std;78 using88int min(01

m: Length of str1 (first string)
n: Length of str2 (second string)
21min(03

x, 1std;78 using88int min(08

m: Length of str1 (first string)
n: Length of str2 (second string)
21min(10

std;90#include 4 min(13

m: Length of str1 (first string)
n: Length of str2 (second string)
21
m: Length of str1 (first string)
n: Length of str2 (second string)
22

int08std;91

std;90

m: Length of str1 (first string)
n: Length of str2 (second string)
01 #include 4 min(21
m: Length of str1 (first string)
n: Length of str2 (second string)
21
m: Length of str1 (first string)
n: Length of str2 (second string)
22

int08std;97

std;90

m: Length of str1 (first string)
n: Length of str2 (second string)
01 #include 4 min(29namespace3
m: Length of str1 (first string)
n: Length of str2 (second string)
22

min(32min(33namespace3

m: Length of str1 (first string)
n: Length of str2 (second string)
40

int08min(37namespace3min(39namespace3int96

std;90

m: Length of str1 (first string)
n: Length of str2 (second string)
01

int08min(45namespace3

min(47min(48namespace3min(50

min(51min(52namespace3min(54

min(51min(52namespace3min(58

min(59min(60namespace3min(62

x, 1std;4

#include 6std;4

#include 6

m: Length of str1 (first string)
n: Length of str2 (second string)
4 int22

#include 3std;4

#include 3

m: Length of str1 (first string)
n: Length of str2 (second string)
72 min(7
m: Length of str1 (first string)
n: Length of str2 (second string)
74
m: Length of str1 (first string)
n: Length of str2 (second string)
75

#include 3#include 2

#include 6

m: Length of str1 (first string)
n: Length of str2 (second string)
79int0int1

#include 6

m: Length of str1 (first string)
n: Length of str2 (second string)
83int4int1

#include 6min(88

x, 1

m: Length of str1 (first string)
n: Length of str2 (second string)
89

#include 3std;4

min(93

Python3

m: Length of str1 (first string)
n: Length of str2 (second string)
93 min(95

#include 3min(97 ____198 min(99

m: Length of str1 (first string)
n: Length of str2 (second string)
21

#include 3std;78 int19int03 int04int13__

#include 6std;78 int28int03 int04int05__

x, 1#include 4 int19

m: Length of str1 (first string)
n: Length of str2 (second string)
98
m: Length of str1 (first string)
n: Length of str2 (second string)
98
m: Length of str1 (first string)
n: Length of str2 (second string)
21#include 01

std;90int43

m: Length of str1 (first string)
n: Length of str2 (second string)
98 int45

x, 1int47 int28

m: Length of str1 (first string)
n: Length of str2 (second string)
98
m: Length of str1 (first string)
n: Length of str2 (second string)
98
m: Length of str1 (first string)
n: Length of str2 (second string)
21#include 01

std;90int43

m: Length of str1 (first string)
n: Length of str2 (second string)
98 int56

x, 1int47 int59#include 18namespace3__

std;90int43

m: Length of str1 (first string)
n: Length of str2 (second string)
98 int72#include 18namespace3int75#include 18namespace3min(58

x, 1

m: Length of str1 (first string)
n: Length of str2 (second string)
01#include 01

std;90int43

m: Length of str1 (first string)
n: Length of str2 (second string)
98 namespace3 #include 39 #include 40__

int92int72#include 18namespace3int96

Các

#include 3

m: Length of str1 (first string)
n: Length of str2 (second string)
4 x, 07

#include 60

m: Length of str1 (first string)
n: Length of str2 (second string)
98 int0

#include 63

m: Length of str1 (first string)
n: Length of str2 (second string)
98 int4

#include 66x, 15#include 68#include 69#include 68#include 71

C#

using #include 73

min(4 #include 75

#include 3min(7 int min(int x, int

m: Length of str1 (first string)
n: Length of str2 (second string)
1int int5

#include 3#include 2

#include 6#include 4 x, 0

x, 1

m: Length of str1 (first string)
n: Length of str2 (second string)
4 x, 3

#include 6#include 4 x, 6

x, 1

m: Length of str1 (first string)
n: Length of str2 (second string)
4 x, 9

#include 6

m: Length of str1 (first string)
n: Length of str2 (second string)
01

x, 1

m: Length of str1 (first string)
n: Length of str2 (second string)
4
m: Length of str1 (first string)
n: Length of str2 (second string)
04

#include 3std;4

#include 3min(7 int int79int

m: Length of str1 (first string)
n: Length of str2 (second string)
12

std;0____6 #include 1

#include 3#include 2

#include 6intx, 68int90 intx, 71

#include 6std;78 using88int std;81

x, 1std;78 using88int std;86

std;90#include 4 std;89

int08x, 86

std;90

m: Length of str1 (first string)
n: Length of str2 (second string)
01 #include 4 std;95

int08x, 92

std;90

m: Length of str1 (first string)
n: Length of str2 (second string)
01 #include 4 int01

int08x, 98

std;90

m: Length of str1 (first string)
n: Length of str2 (second string)
01

int08

m: Length of str1 (first string)
n: Length of str2 (second string)
002

min(47

m: Length of str1 (first string)
n: Length of str2 (second string)
004

min(51

m: Length of str1 (first string)
n: Length of str2 (second string)
006

min(51

m: Length of str1 (first string)
n: Length of str2 (second string)
008

m: Length of str1 (first string)
n: Length of str2 (second string)
009
m: Length of str1 (first string)
n: Length of str2 (second string)
010

x, 1std;4

#include 6std;4

#include 6

m: Length of str1 (first string)
n: Length of str2 (second string)
4
m: Length of str1 (first string)
n: Length of str2 (second string)
017

#include 3std;4

#include 3

m: Length of str1 (first string)
n: Length of str2 (second string)
72 min(7
m: Length of str1 (first string)
n: Length of str2 (second string)
74 using55

#include 3#include 2

#include 6

m: Length of str1 (first string)
n: Length of str2 (second string)
79int0int1

#include 6

m: Length of str1 (first string)
n: Length of str2 (second string)
83int4int1

#include 6

m: Length of str1 (first string)
n: Length of str2 (second string)
036

m: Length of str1 (first string)
n: Length of str2 (second string)
037
m: Length of str1 (first string)
n: Length of str2 (second string)
038

#include 3std;4

std;4

PHP

using73

using74

m: Length of str1 (first string)
n: Length of str2 (second string)
044using76using77 ____378
m: Length of str1 (first string)
n: Length of str2 (second string)
62

int08using81using77using83

m: Length of str1 (first string)
n: Length of str2 (second string)
22

#include 2

std;78 using88

m: Length of str1 (first string)
n: Length of str2 (second string)
057 ____1058
m: Length of str1 (first string)
n: Length of str2 (second string)
057

m: Length of str1 (first string)
n: Length of str2 (second string)
065

Is

#include 3#include 2

#include 6#include 4 using88

m: Length of str1 (first string)
n: Length of str2 (second string)
057 using90

Các

#include 6

m: Length of str1 (first string)
n: Length of str2 (second string)
01 #include 4using88
m: Length of str1 (first string)
n: Length of str2 (second string)
069 using90

x, 1

m: Length of str1 (first string)
n: Length of str2 (second string)
085namespace08
m: Length of str1 (first string)
n: Length of str2 (second string)
057
m: Length of str1 (first string)
n: Length of str2 (second string)
088
m: Length of str1 (first string)
n: Length of str2 (second string)
069
m: Length of str1 (first string)
n: Length of str2 (second string)
090
m: Length of str1 (first string)
n: Length of str2 (second string)
057
m: Length of str1 (first string)
n: Length of str2 (second string)
062

#include 6

m: Length of str1 (first string)
n: Length of str2 (second string)
01 #include 4using88using76____408__

Các

#include 6

m: Length of str1 (first string)
n: Length of str2 (second string)
01

#include 6#include 2

Các

m: Length of str1 (first string)
n: Length of str2 (second string)
150
m: Length of str1 (first string)
n: Length of str2 (second string)
085namespace08
m: Length of str1 (first string)
n: Length of str2 (second string)
057
m: Length of str1 (first string)
n: Length of str2 (second string)
130__1069
m: Length of str1 (first string)
n: Length of str2 (second string)
156

m: Length of str1 (first string)
n: Length of str2 (second string)
150
m: Length of str1 (first string)
n: Length of str2 (second string)
085namespace08
m: Length of str1 (first string)
n: Length of str2 (second string)
057
m: Length of str1 (first string)
n: Length of str2 (second string)
130
m: Length of str1 (first string)
n: Length of str2 (second string)
069
m: Length of str1 (first string)
n: Length of str2 (second string)
163

#include 6std;4

#include 3std;4

std;4

m: Length of str1 (first string)
n: Length of str2 (second string)
4
m: Length of str1 (first string)
n: Length of str2 (second string)
085namespace08using81
m: Length of str1 (first string)
n: Length of str2 (second string)
088using83
m: Length of str1 (first string)
n: Length of str2 (second string)
175

std;4

using76 namespace67int0int1

using78 namespace67int4int1

namespace74

m: Length of str1 (first string)
n: Length of str2 (second string)
044using76using77 ____378using777____480using88using76namespace83

m: Length of str1 (first string)
n: Length of str2 (second string)
195namespace80using88using78namespace88

namespace89

JavaScript

namespace90

using74

m: Length of str1 (first string)
n: Length of str2 (second string)
203

#include 2

#include 3#include 4 x, 0

x, 1

m: Length of str1 (first string)
n: Length of str2 (second string)
4 x, 3

#include 6#include 4 x, 6

x, 1

m: Length of str1 (first string)
n: Length of str2 (second string)
4 x, 9

#include 6

m: Length of str1 (first string)
n: Length of str2 (second string)
01

x, 1

m: Length of str1 (first string)
n: Length of str2 (second string)
4
m: Length of str1 (first string)
n: Length of str2 (second string)
04

std;4

using74

m: Length of str1 (first string)
n: Length of str2 (second string)
224

#include 2

#include 6

m: Length of str1 (first string)
n: Length of str2 (second string)
227int90
m: Length of str1 (first string)
n: Length of str2 (second string)
229

#include 6std;78

m: Length of str1 (first string)
n: Length of str2 (second string)
232

#include 6#include 2

x, 1

m: Length of str1 (first string)
n: Length of str2 (second string)
236int90
m: Length of str1 (first string)
n: Length of str2 (second string)
238

x, 1std;78

m: Length of str1 (first string)
n: Length of str2 (second string)
241

x, 1#include 2

std;90

m: Length of str1 (first string)
n: Length of str2 (second string)
245

x, 1std;4

#include 6std;4

#include 6std;78

m: Length of str1 (first string)
n: Length of str2 (second string)
252

x, 1std;78

m: Length of str1 (first string)
n: Length of str2 (second string)
255

std;90#include 4 std;89

int08std;91

std;90

m: Length of str1 (first string)
n: Length of str2 (second string)
01 #include 4 std;95

int08std;97

std;90

m: Length of str1 (first string)
n: Length of str2 (second string)
01 #include 4
m: Length of str1 (first string)
n: Length of str2 (second string)
270

min(32

m: Length of str1 (first string)
n: Length of str2 (second string)
272

int08int03

std;90

m: Length of str1 (first string)
n: Length of str2 (second string)
01

int08

m: Length of str1 (first string)
n: Length of str2 (second string)
278

min(47int11

min(51int13

min(51

m: Length of str1 (first string)
n: Length of str2 (second string)
284

min(59

m: Length of str1 (first string)
n: Length of str2 (second string)
286

x, 1std;4

#include 6std;4

#include 6

m: Length of str1 (first string)
n: Length of str2 (second string)
4 int22

std;4

std;43int0int1

std;46int4int1

m: Length of str1 (first string)
n: Length of str2 (second string)
301

m: Length of str1 (first string)
n: Length of str2 (second string)
302

std;52

Độ phức tạp về thời gian: O (M x N) & NBSP; Không gian phụ trợ: O (M x N) O(m x n) 
Auxiliary Space: O(m x n)

Giải pháp phức tạp không gian: Trong phương pháp được đưa ra trên, chúng tôi yêu cầu không gian O (M x N). Điều này sẽ không phù hợp nếu độ dài của chuỗi lớn hơn 2000 vì nó chỉ có thể tạo mảng 2D 2000 x 2000. Để lấp đầy một hàng trong mảng DP, chúng tôi chỉ yêu cầu một hàng trên trên. Ví dụ: nếu chúng ta đang điền vào các hàng I = 10 trong mảng DP, chúng ta chỉ yêu cầu các giá trị của hàng thứ 9. Vì vậy, chúng tôi chỉ cần tạo một mảng DP có chiều dài 2 x str1. Cách tiếp cận này làm giảm sự phức tạp không gian. Đây là việc thực hiện C ++ của vấn đề nêu trên: In the above-given method we require O(m x n) space. This will not be suitable if the length of strings is greater than 2000 as it can only create 2D array of 2000 x 2000. To fill a row in DP array we require only one row the upper row. For example, if we are filling the i = 10 rows in DP array we require only values of 9th row. So we simply create a DP array of 2 x str1 length. This approach reduces the space complexity. Here is the C++ implementation of the above-mentioned problem

C++

#include

using namespace std;

m: Length of str1 (first string)
n: Length of str2 (second string)
74
m: Length of str1 (first string)
n: Length of str2 (second string)
309

#include 2

#include 3int

m: Length of str1 (first string)
n: Length of str2 (second string)
313

#include 3int

m: Length of str1 (first string)
n: Length of str2 (second string)
316

#include 3int

m: Length of str1 (first string)
n: Length of str2 (second string)
319

#include 3

m: Length of str1 (first string)
n: Length of str2 (second string)
321
m: Length of str1 (first string)
n: Length of str2 (second string)
32222323
m: Length of str1 (first string)
n: Length of str2 (second string)
324

#include 3std;78 using88int

m: Length of str1 (first string)
n: Length of str2 (second string)
329

#include 6

m: Length of str1 (first string)
n: Length of str2 (second string)
331

#include 3std;78 using88int

m: Length of str1 (first string)
n: Length of str2 (second string)
336

#include 6std;78 using88int

m: Length of str1 (first string)
n: Length of str2 (second string)
341

x, 1#include 4 std;95

std;90

m: Length of str1 (first string)
n: Length of str2 (second string)
346

x, 1

m: Length of str1 (first string)
n: Length of str2 (second string)
01 #include 4
m: Length of str1 (first string)
n: Length of str2 (second string)
350

std;90

m: Length of str1 (first string)
n: Length of str2 (second string)
352

x, 1std;4

x, 1

m: Length of str1 (first string)
n: Length of str2 (second string)
01 #include 2

std;90

m: Length of str1 (first string)
n: Length of str2 (second string)
359

min(59

m: Length of str1 (first string)
n: Length of str2 (second string)
361

m: Length of str1 (first string)
n: Length of str2 (second string)
362
m: Length of str1 (first string)
n: Length of str2 (second string)
363

x, 1std;4

#include 6std;4

#include 3std;4

#include 3

m: Length of str1 (first string)
n: Length of str2 (second string)
371

std;4

int std;6

#include 2

#include 3std;9

m: Length of str1 (first string)
n: Length of str2 (second string)
378int1

#include 3int3

m: Length of str1 (first string)
n: Length of str2 (second string)
382int1

#include 3

m: Length of str1 (first string)
n: Length of str2 (second string)
385

#include 3

m: Length of str1 (first string)
n: Length of str2 (second string)
4 min(2

std;4

Java

m: Length of str1 (first string)
n: Length of str2 (second string)
390
m: Length of str1 (first string)
n: Length of str2 (second string)
391

min(4

m: Length of str1 (first string)
n: Length of str2 (second string)
393

#include 2

min(7

m: Length of str1 (first string)
n: Length of str2 (second string)
74
m: Length of str1 (first string)
n: Length of str2 (second string)
397

#include 2

#include 3int

m: Length of str1 (first string)
n: Length of str2 (second string)
313

#include 3int

m: Length of str1 (first string)
n: Length of str2 (second string)
316

#include 3int

m: Length of str1 (first string)
n: Length of str2 (second string)
319

#include 3

m: Length of str1 (first string)
n: Length of str2 (second string)
321
m: Length of str1 (first string)
n: Length of str2 (second string)
32222323
m: Length of str1 (first string)
n: Length of str2 (second string)
324

#include 6

m: Length of str1 (first string)
n: Length of str2 (second string)
423
m: Length of str1 (first string)
n: Length of str2 (second string)
21
m: Length of str1 (first string)
n: Length of str2 (second string)
425

#include 3std;78 using88int

m: Length of str1 (first string)
n: Length of str2 (second string)
329

#include 3#include 2

#include 3std;78 using88int

m: Length of str1 (first string)
n: Length of str2 (second string)
336

#include 6#include 2

#include 6std;78 using88int

m: Length of str1 (first string)
n: Length of str2 (second string)
341

std;90

m: Length of str1 (first string)
n: Length of str2 (second string)
450
m: Length of str1 (first string)
n: Length of str2 (second string)
411
m: Length of str1 (first string)
n: Length of str2 (second string)
452

x, 1#include 4 std;95

std;90

m: Length of str1 (first string)
n: Length of str2 (second string)
450
m: Length of str1 (first string)
n: Length of str2 (second string)
411
m: Length of str1 (first string)
n: Length of str2 (second string)
464namespace3
m: Length of str1 (first string)
n: Length of str2 (second string)
466
m: Length of str1 (first string)
n: Length of str2 (second string)
411min(39namespace3int96

x, 1std;4

x, 1

m: Length of str1 (first string)
n: Length of str2 (second string)
01 #include 2

min(59

m: Length of str1 (first string)
n: Length of str2 (second string)
361

min(59

m: Length of str1 (first string)
n: Length of str2 (second string)
487
m: Length of str1 (first string)
n: Length of str2 (second string)
411min(39namespace3
m: Length of str1 (first string)
n: Length of str2 (second string)
491

m: Length of str1 (first string)
n: Length of str2 (second string)
362
m: Length of str1 (first string)
n: Length of str2 (second string)
493namespace3
m: Length of str1 (first string)
n: Length of str2 (second string)
466
m: Length of str1 (first string)
n: Length of str2 (second string)
411min(39namespace3
m: Length of str1 (first string)
n: Length of str2 (second string)
499

x, 1std;4

#include 6std;4

#include 3std;4

int std;6

std;4

#include 3

m: Length of str1 (first string)
n: Length of str2 (second string)
4 min(2

#include 2

#include 3

m: Length of str1 (first string)
n: Length of str2 (second string)
79
m: Length of str1 (first string)
n: Length of str2 (second string)
378int1

#include 3

m: Length of str1 (first string)
n: Length of str2 (second string)
83
m: Length of str1 (first string)
n: Length of str2 (second string)
382int1

#include 3

m: Length of str1 (first string)
n: Length of str2 (second string)
385

std;4

std;4

Python3

Java

m: Length of str1 (first string)
n: Length of str2 (second string)
390
m: Length of str1 (first string)
n: Length of str2 (second string)
391

#include 3

m: Length of str1 (first string)
n: Length of str2 (second string)
538
m: Length of str1 (first string)
n: Length of str2 (second string)
98 #include 68
m: Length of str1 (first string)
n: Length of str2 (second string)
541

#include 3

m: Length of str1 (first string)
n: Length of str2 (second string)
543______198

std;2std;78 int28int03 int04using88

m: Length of str1 (first string)
n: Length of str2 (second string)
411
m: Length of str1 (first string)
n: Length of str2 (second string)
562

#include 3std;78 int19______803

#include 6

m: Length of str1 (first string)
n: Length of str2 (second string)
423
m: Length of str1 (first string)
n: Length of str2 (second string)
21
m: Length of str1 (first string)
n: Length of str2 (second string)
577
m: Length of str1 (first string)
n: Length of str2 (second string)
98
m: Length of str1 (first string)
n: Length of str2 (second string)
579

#include 3std;78 int19______803

#include 6std;78 int28______803

x, 1#include 4

m: Length of str1 (first string)
n: Length of str2 (second string)
604
m: Length of str1 (first string)
n: Length of str2 (second string)
98
m: Length of str1 (first string)
n: Length of str2 (second string)
98
m: Length of str1 (first string)
n: Length of str2 (second string)
21int25

std;90

m: Length of str1 (first string)
n: Length of str2 (second string)
610
m: Length of str1 (first string)
n: Length of str2 (second string)
611
m: Length of str1 (first string)
n: Length of str2 (second string)
411
m: Length of str1 (first string)
n: Length of str2 (second string)
613
m: Length of str1 (first string)
n: Length of str2 (second string)
98
m: Length of str1 (first string)
n: Length of str2 (second string)
579

x, 1int47

m: Length of str1 (first string)
n: Length of str2 (second string)
618#include 18 namespace3__

std;90

m: Length of str1 (first string)
n: Length of str2 (second string)
610
m: Length of str1 (first string)
n: Length of str2 (second string)
611
m: Length of str1 (first string)
n: Length of str2 (second string)
411
m: Length of str1 (first string)
n: Length of str2 (second string)
613__

x, 1

m: Length of str1 (first string)
n: Length of str2 (second string)
01#include 01

std;90

m: Length of str1 (first string)
n: Length of str2 (second string)
610
m: Length of str1 (first string)
n: Length of str2 (second string)
611
m: Length of str1 (first string)
n: Length of str2 (second string)
411
m: Length of str1 (first string)
n: Length of str2 (second string)
613__

std;50#include 40

m: Length of str1 (first string)
n: Length of str2 (second string)
666
m: Length of str1 (first string)
n: Length of str2 (second string)
611
m: Length of str1 (first string)
n: Length of str2 (second string)
411
m: Length of str1 (first string)
n: Length of str2 (second string)
640______

m: Length of str1 (first string)
n: Length of str2 (second string)
150
m: Length of str1 (first string)
n: Length of str2 (second string)
634
m: Length of str1 (first string)
n: Length of str2 (second string)
8

#include 3#include 66

m: Length of str1 (first string)
n: Length of str2 (second string)
686
m: Length of str1 (first string)
n: Length of str2 (second string)
611
m: Length of str1 (first string)
n: Length of str2 (second string)
411
m: Length of str1 (first string)
n: Length of str2 (second string)
689

#include 4

m: Length of str1 (first string)
n: Length of str2 (second string)
691
m: Length of str1 (first string)
n: Length of str2 (second string)
98
m: Length of str1 (first string)
n: Length of str2 (second string)
98
m: Length of str1 (first string)
n: Length of str2 (second string)
694#include 01

#include 3#include 60

m: Length of str1 (first string)
n: Length of str2 (second string)
98
m: Length of str1 (first string)
n: Length of str2 (second string)
378

#include 3#include 63

m: Length of str1 (first string)
n: Length of str2 (second string)
98
m: Length of str1 (first string)
n: Length of str2 (second string)
382

#include 3

m: Length of str1 (first string)
n: Length of str2 (second string)
705

C#

using #include 73

min(4

m: Length of str1 (first string)
n: Length of str2 (second string)
393

#include 2

min(7

m: Length of str1 (first string)
n: Length of str2 (second string)
74
m: Length of str1 (first string)
n: Length of str2 (second string)
397

#include 2

#include 3int

m: Length of str1 (first string)
n: Length of str2 (second string)
717

#include 3int

m: Length of str1 (first string)
n: Length of str2 (second string)
720

#include 3int

m: Length of str1 (first string)
n: Length of str2 (second string)
723int90 int
m: Length of str1 (first string)
n: Length of str2 (second string)
726

#include 3std;78 using88int

m: Length of str1 (first string)
n: Length of str2 (second string)
329

#include 6

m: Length of str1 (first string)
n: Length of str2 (second string)
733

#include 3std;78 using88int

m: Length of str1 (first string)
n: Length of str2 (second string)
738

#include 3#include 2

#include 6std;78 using88int

m: Length of str1 (first string)
n: Length of str2 (second string)
745

#include 6#include 2

x, 1#include 4 std;95

std;90

m: Length of str1 (first string)
n: Length of str2 (second string)
752

x, 1

m: Length of str1 (first string)
n: Length of str2 (second string)
01 #include 4
m: Length of str1 (first string)
n: Length of str2 (second string)
756

x, 1#include 2

std;90

m: Length of str1 (first string)
n: Length of str2 (second string)
760

x, 1std;4

x, 1

m: Length of str1 (first string)
n: Length of str2 (second string)
01 #include 2

std;90

m: Length of str1 (first string)
n: Length of str2 (second string)
767

min(59

m: Length of str1 (first string)
n: Length of str2 (second string)
769

m: Length of str1 (first string)
n: Length of str2 (second string)
362
m: Length of str1 (first string)
n: Length of str2 (second string)
771

x, 1std;4

#include 6std;4

#include 3std;4

#include 3

m: Length of str1 (first string)
n: Length of str2 (second string)
779
m: Length of str1 (first string)
n: Length of str2 (second string)
510std;3

std;4

m: Length of str1 (first string)
n: Length of str2 (second string)
72 min(7
m: Length of str1 (first string)
n: Length of str2 (second string)
74
m: Length of str1 (first string)
n: Length of str2 (second string)
786

#include 2

#include 3

m: Length of str1 (first string)
n: Length of str2 (second string)
79
m: Length of str1 (first string)
n: Length of str2 (second string)
378int1

#include 3

m: Length of str1 (first string)
n: Length of str2 (second string)
83
m: Length of str1 (first string)
n: Length of str2 (second string)
382int1

#include 3

m: Length of str1 (first string)
n: Length of str2 (second string)
385

std;4

std;4

JavaScript

namespace90

using74

m: Length of str1 (first string)
n: Length of str2 (second string)
705

#include 2

#include 3

m: Length of str1 (first string)
n: Length of str2 (second string)
805

#include 3

m: Length of str1 (first string)
n: Length of str2 (second string)
807

#include 3

m: Length of str1 (first string)
n: Length of str2 (second string)
809int90
m: Length of str1 (first string)
n: Length of str2 (second string)
811

#include 3std;78

m: Length of str1 (first string)
n: Length of str2 (second string)
814

#include 3#include 2

#include 6

m: Length of str1 (first string)
n: Length of str2 (second string)
818int90
m: Length of str1 (first string)
n: Length of str2 (second string)
820

#include 6std;78

m: Length of str1 (first string)
n: Length of str2 (second string)
823

x, 1

m: Length of str1 (first string)
n: Length of str2 (second string)
825

#include 3std;4

#include 3std;78

m: Length of str1 (first string)
n: Length of str2 (second string)
830

#include 6

m: Length of str1 (first string)
n: Length of str2 (second string)
331

#include 3std;78

m: Length of str1 (first string)
n: Length of str2 (second string)
835

#include 3#include 2

#include 6std;78

m: Length of str1 (first string)
n: Length of str2 (second string)
840

#include 6#include 2

x, 1#include 4 std;95

x, 1

m: Length of str1 (first string)
n: Length of str2 (second string)
01 #include 4
m: Length of str1 (first string)
n: Length of str2 (second string)
756

x, 1

m: Length of str1 (first string)
n: Length of str2 (second string)
01 #include 2

std;90

m: Length of str1 (first string)
n: Length of str2 (second string)
352

x, 1std;4

x, 1

m: Length of str1 (first string)
n: Length of str2 (second string)
01 #include 2

std;90

m: Length of str1 (first string)
n: Length of str2 (second string)
860

min(59

m: Length of str1 (first string)
n: Length of str2 (second string)
862

m: Length of str1 (first string)
n: Length of str2 (second string)
362
m: Length of str1 (first string)
n: Length of str2 (second string)
363

x, 1std;4

#include 6std;4

#include 3std;4

#include 3

m: Length of str1 (first string)
n: Length of str2 (second string)
872
m: Length of str1 (first string)
n: Length of str2 (second string)
873std;3

std;4

std;43

m: Length of str1 (first string)
n: Length of str2 (second string)
378int1

std;46

m: Length of str1 (first string)
n: Length of str2 (second string)
382int1

m: Length of str1 (first string)
n: Length of str2 (second string)
385

std;52

m: Length of str1 (first string)
n: Length of str2 (second string)
72 min(7
m: Length of str1 (first string)
n: Length of str2 (second string)
74
m: Length of str1 (first string)
n: Length of str2 (second string)
786
O(m x n) 
Auxiliary Space: O( m )

JavaScript

C++14

#include

namespace90

using74

m: Length of str1 (first string)
n: Length of str2 (second string)
705

namespace4

m: Length of str1 (first string)
n: Length of str2 (second string)
895int
m: Length of str1 (first string)
n: Length of str2 (second string)
897

#include 2

#include 3

m: Length of str1 (first string)
n: Length of str2 (second string)
809int90
m: Length of str1 (first string)
n: Length of str2 (second string)
811

#include 6

m: Length of str1 (first string)
n: Length of str2 (second string)
818int90
m: Length of str1 (first string)
n: Length of str2 (second string)
820

#include 3std;78

m: Length of str1 (first string)
n: Length of str2 (second string)
830

#include 3std;78

m: Length of str1 (first string)
n: Length of str2 (second string)
835

#include 6std;78

m: Length of str1 (first string)
n: Length of str2 (second string)
840

std;90

m: Length of str1 (first string)
n: Length of str2 (second string)
346

x, 1

m: Length of str1 (first string)
n: Length of str2 (second string)
01 #include 4
m: Length of str1 (first string)
n: Length of str2 (second string)
851

Độ phức tạp về thời gian: O (m x n) & nbsp; không gian phụ trợ: O (m)

#include 3std;4

Đây là phiên bản ghi nhớ của đệ quy, tức là DP từ trên xuống:

using namespace std;

#include 6

m: Length of str1 (first string)
n: Length of str2 (second string)
932

#include 6

m: Length of str1 (first string)
n: Length of str2 (second string)
934

#include 6

m: Length of str1 (first string)
n: Length of str2 (second string)
936

#include 6

m: Length of str1 (first string)
n: Length of str2 (second string)
4
m: Length of str1 (first string)
n: Length of str2 (second string)
939

#include 3std;4

std;4

int std;6

#include 2

#include 3std;9

m: Length of str1 (first string)
n: Length of str2 (second string)
948int1

#include 3int3

m: Length of str1 (first string)
n: Length of str2 (second string)
952int1

#include 3int

m: Length of str1 (first string)
n: Length of str2 (second string)
956

#include 3

m: Length of str1 (first string)
n: Length of str2 (second string)
895int
m: Length of str1 (first string)
n: Length of str2 (second string)
960int
m: Length of str1 (first string)
n: Length of str2 (second string)
962

#include 3

m: Length of str1 (first string)
n: Length of str2 (second string)
964

#include 3

m: Length of str1 (first string)
n: Length of str2 (second string)
4 min(2

std;4

Java

m: Length of str1 (first string)
n: Length of str2 (second string)
390
m: Length of str1 (first string)
n: Length of str2 (second string)
391

min(4 #include 75

#include 3min(7 int

m: Length of str1 (first string)
n: Length of str2 (second string)
976int
m: Length of str1 (first string)
n: Length of str2 (second string)
891int
m: Length of str1 (first string)
n: Length of str2 (second string)
12

using80int

m: Length of str1 (first string)
n: Length of str2 (second string)
983

#include 3#include 2

#include 6#include 4

m: Length of str1 (first string)
n: Length of str2 (second string)
28
m: Length of str1 (first string)
n: Length of str2 (second string)
21
m: Length of str1 (first string)
n: Length of str2 (second string)
22

x, 1

m: Length of str1 (first string)
n: Length of str2 (second string)
4 using4

#include 6#include 4

m: Length of str1 (first string)
n: Length of str2 (second string)
20
m: Length of str1 (first string)
n: Length of str2 (second string)
21
m: Length of str1 (first string)
n: Length of str2 (second string)
22

x, 1

m: Length of str1 (first string)
n: Length of str2 (second string)
4 #include 8

#include 6#include 4 #include 004namespace3

m: Length of str1 (first string)
n: Length of str2 (second string)
22

x, 1

m: Length of str1 (first string)
n: Length of str2 (second string)
4
m: Length of str1 (first string)
n: Length of str2 (second string)
916

#include 6#include 4 #include 012namespace3#include 014namespace3

m: Length of str1 (first string)
n: Length of str2 (second string)
460

x, 1

m: Length of str1 (first string)
n: Length of str2 (second string)
4 #include 019namespace3#include 021namespace3#include 023

#include 6std;4

#include 6

m: Length of str1 (first string)
n: Length of str2 (second string)
01 #include 2

x, 1int

m: Length of str1 (first string)
n: Length of str2 (second string)
930

x, 1#include 033namespace3#include 023

x, 1#include 037namespace3#include 039

x, 1#include 041namespace3#include 021namespace3#include 023

x, 1

m: Length of str1 (first string)
n: Length of str2 (second string)
4 #include 048

std;90namespace67namespace3 #include 052

#include 6std;4

#include 3std;4

#include 3

m: Length of str1 (first string)
n: Length of str2 (second string)
72 min(7
m: Length of str1 (first string)
n: Length of str2 (second string)
74
m: Length of str1 (first string)
n: Length of str2 (second string)
516

#include 3#include 2

#include 6

m: Length of str1 (first string)
n: Length of str2 (second string)
79
m: Length of str1 (first string)
n: Length of str2 (second string)
948int1

#include 6

m: Length of str1 (first string)
n: Length of str2 (second string)
83
m: Length of str1 (first string)
n: Length of str2 (second string)
952int1

#include 6int

m: Length of str1 (first string)
n: Length of str2 (second string)
956

#include 6int#include 0777 ____690 int#include 080namespace3__

#include 6std;78 using88int min(01

m: Length of str1 (first string)
n: Length of str2 (second string)
21#include 091namespace3#include 093

x, 1#include 095namespace3std;3

#include 6#include 099

#include 3std;4

std;4

Python3

m: Length of str1 (first string)
n: Length of str2 (second string)
93 #include 104

#include 105#include 4int05

m: Length of str1 (first string)
n: Length of str2 (second string)
98
m: Length of str1 (first string)
n: Length of str2 (second string)
98
m: Length of str1 (first string)
n: Length of str2 (second string)
21#include 111

#include 1122 #include 114

#include 105#include 4int13

m: Length of str1 (first string)
n: Length of str2 (second string)
98
m: Length of str1 (first string)
n: Length of str2 (second string)
98
m: Length of str1 (first string)
n: Length of str2 (second string)
21#include 111

#include 1122 #include 04

#include 105#include 4#include 127

m: Length of str1 (first string)
n: Length of str2 (second string)
98 #include 18namespace3#include 131

#include 112

m: Length of str1 (first string)
n: Length of str2 (second string)
4
m: Length of str1 (first string)
n: Length of str2 (second string)
916

#include 105#include 4#include 137#include 18 namespace3#include 20__

#include 3#include 4#include 149#include 18

#include 6#include 162

m: Length of str1 (first string)
n: Length of str2 (second string)
98 #include 164#include 18 namespace3__

#include 6

m: Length of str1 (first string)
n: Length of str2 (second string)
4 #include 173

#include 3

m: Length of str1 (first string)
n: Length of str2 (second string)
01 #include 01

#include 6#include 162

m: Length of str1 (first string)
n: Length of str2 (second string)
98 #include 180__218

#include 6

m: Length of str1 (first string)
n: Length of str2 (second string)
4 #include 048

#include 105

m: Length of str1 (first string)
n: Length of str2 (second string)
01 #include 192

Các

#include 1122____2204

m: Length of str1 (first string)
n: Length of str2 (second string)
98 #include 180__218 namespace3#include 209

#include 3

m: Length of str1 (first string)
n: Length of str2 (second string)
01 #include 01

#include 6#include 162

m: Length of str1 (first string)
n: Length of str2 (second string)
98 #include 180__218

#include 6

m: Length of str1 (first string)
n: Length of str2 (second string)
4 #include 048

#include 105

m: Length of str1 (first string)
n: Length of str2 (second string)
01 #include 192

#include 3

m: Length of str1 (first string)
n: Length of str2 (second string)
01 #include 01

#include 6#include 162

m: Length of str1 (first string)
n: Length of str2 (second string)
98 #include 180__218

#include 6

m: Length of str1 (first string)
n: Length of str2 (second string)
4 #include 048

#include 105

m: Length of str1 (first string)
n: Length of str2 (second string)
01 #include 192

#include 3

m: Length of str1 (first string)
n: Length of str2 (second string)
01 #include 01

#include 6#include 162

m: Length of str1 (first string)
n: Length of str2 (second string)
98 #include 180__218

#include 6

m: Length of str1 (first string)
n: Length of str2 (second string)
4 #include 048

#include 105

m: Length of str1 (first string)
n: Length of str2 (second string)
01 #include 192

Các

#include 1122____2204

m: Length of str1 (first string)
n: Length of str2 (second string)
98 #include 180__218 namespace3#include 209

#include 1122#include 204

m: Length of str1 (first string)
n: Length of str2 (second string)
98 #include 164#include 18 namespace3#include 219

#include 3#include 4#include 2222218 namespace3#include 225

m: Length of str1 (first string)
n: Length of str2 (second string)
98 #include 18__

#include 1122#include 231

m: Length of str1 (first string)
n: Length of str2 (second string)
98 #include 233#include 18 namespace3#include 2236

#include 66#include 331

C#

#include 1122#include 231

m: Length of str1 (first string)
n: Length of str2 (second string)
98 #include 243#include 18 namespace3#include 246

#include 3#include 4#include 149#include 18

min(4 #include 75

#include 105min(7 int #include 341#include 342 #include 343#include 342

int08int #include 350int#include 352

#include 105#include 2

#include 3#include 4#include 357

#include 1122____14 using4

#include 3#include 4#include 363

#include 1122 #include 8

#include 3#include 4#include 369

#include 112

m: Length of str1 (first string)
n: Length of str2 (second string)
4
m: Length of str1 (first string)
n: Length of str2 (second string)
916

#include 3#include 4#include 375

#include 3#include 377

#include 112#include 4#include 380

#include 112#include 382

#include 6

m: Length of str1 (first string)
n: Length of str2 (second string)
4 #include 385

#include 112#include 387

#include 112

m: Length of str1 (first string)
n: Length of str2 (second string)
01

#include 6

m: Length of str1 (first string)
n: Length of str2 (second string)
4 #include 392

#include 3std;4

#include 3

m: Length of str1 (first string)
n: Length of str2 (second string)
01

#include 3#include 377

#include 112int #include 401

#include 112#include 4#include 404

#include 1122402406

#include 6#include 408

#include 112#include 410

#include 112

m: Length of str1 (first string)
n: Length of str2 (second string)
01

#include 112#include 414

#include 6#include 416

#include 112#include 418

#include 112#include 4#include 421

#include 112#include 423

#include 6#include 425

#include 112#include 418

#include 112

m: Length of str1 (first string)
n: Length of str2 (second string)
01

#include 112#include 406

#include 6#include 433

#include 112#include 435

#include 112#include 4#include 438

#include 112#include 406

#include 6#include 442

#include 112#include 444

#include 112

m: Length of str1 (first string)
n: Length of str2 (second string)
01

#include 112#include 414

#include 6#include 450

#include 112#include 452

#include 112

m: Length of str1 (first string)
n: Length of str2 (second string)
4 #include 455

#include 3std;4

#include 105std;4

#include 105min(7

m: Length of str1 (first string)
n: Length of str2 (second string)
74 using55

#include 105#include 2

#include 3#include 342 #include 468

m: Length of str1 (first string)
n: Length of str2 (second string)
948int1

#include 3#include 342 #include 473

m: Length of str1 (first string)
n: Length of str2 (second string)
952int1

#include 3int #include 478

Các

#include 3std;78using88int #include 491

#include 3#include 2

Các

#include 112#include 2

#include 6#include 508

#include 112std;4

#include 3std;4

#include 3#include 514

#include 105std;4

std;4

JavaScript

namespace90

using74 #include 520

#include 2

#include 105#include 4#include 357

#include 3

m: Length of str1 (first string)
n: Length of str2 (second string)
4 using4

#include 105#include 4#include 530

#include 3

m: Length of str1 (first string)
n: Length of str2 (second string)
4 #include 8

#include 105#include 4#include 536

#include 3

m: Length of str1 (first string)
n: Length of str2 (second string)
4
m: Length of str1 (first string)
n: Length of str2 (second string)
916

#include 105#include 4#include 375

#include 105#include 544

#include 3#include 4#include 380

#include 3#include 549

#include 112

m: Length of str1 (first string)
n: Length of str2 (second string)
4 #include 552

#include 3#include 554

#include 3

m: Length of str1 (first string)
n: Length of str2 (second string)
01

#include 112

m: Length of str1 (first string)
n: Length of str2 (second string)
4 #include 559

#include 105std;4

#include 105

m: Length of str1 (first string)
n: Length of str2 (second string)
01

#include 105#include 544

#include 3#include 567

#include 3#include 4#include 570

#include 3#include 414

#include 112#include 574

#include 3#include 576

#include 3

m: Length of str1 (first string)
n: Length of str2 (second string)
01

#include 3

m: Length of str1 (first string)
n: Length of str2 (second string)
065

#include 112#include 582

#include 3#include 410

#include 3#include 4#include 421

#include 3#include 382

#include 112#include 591

#include 3#include 410

#include 3

m: Length of str1 (first string)
n: Length of str2 (second string)
01

#include 3#include 414

#include 112#include 599

#include 3#include 601

#include 3#include 4#include 438

#include 3#include 414

#include 112#include 608

#include 3#include 610

#include 3

m: Length of str1 (first string)
n: Length of str2 (second string)
01

#include 3

m: Length of str1 (first string)
n: Length of str2 (second string)
065

#include 112#include 616

#include 3#include 618

#include 3

m: Length of str1 (first string)
n: Length of str2 (second string)
4 #include 621

#include 105std;4

std;4

std;43

m: Length of str1 (first string)
n: Length of str2 (second string)
948int1

std;46

m: Length of str1 (first string)
n: Length of str2 (second string)
952int1

#include 631

m: Length of str1 (first string)
n: Length of str2 (second string)
227int90 #include 634

std;78#include 636

#include 2

#include 3

m: Length of str1 (first string)
n: Length of str2 (second string)
236int90 #include 641

#include 3std;78#include 644

#include 6#include 646

std;4

#include 648

std;52

Độ phức tạp về thời gian: O (m x n) & nbsp; không gian phụ trợ: o (m *n)+o (m+n) & nbsp; O(m x n) 
Auxiliary Space: O( m *n)+O(m+n)  

.

Ứng dụng: Có nhiều ứng dụng thực tế của thuật toán chỉnh sửa khoảng cách, hãy giới thiệu API Lucene cho mẫu. Một ví dụ khác, hiển thị tất cả các từ trong một từ điển gần gần với một từ được đánh vần là từ ngữ nhất định.: There are many practical applications of edit distance algorithm, refer Lucene API for sample. Another example, display all the words in a dictionary that are near proximity to a given wordincorrectly spelled word.

Cảm ơn Vivek Kumar vì đã đề xuất các bản cập nhật. Cảm ơn đến Venki đã cung cấp bài đăng ban đầu. Vui lòng viết nhận xét nếu bạn tìm thấy bất cứ điều gì không chính xác hoặc bạn muốn chia sẻ thêm thông tin về chủ đề được thảo luận ở trên & NBSP;
Thanks to Venki for providing initial post. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above