Hướng dẫn deletion distance python - xóa khoảng cách python

Tôi đã giải quyết vấn đề này tại Pramp và tôi gặp khó khăn trong việc tìm ra thuật toán cho vấn đề này.

Nội phân Chính showShow

  • Mã ví dụ
  • Làm thế nào để Python tính khoảng cách chỉnh sửa?
  • Khoảng cách chỉnh sửa tối thiểu giữa hai chuỗi là bao nhiêu?
  • Làm thế nào để bạn tìm thấy khoảng cách giữa hai chữ cái trong Python?
  • Làm thế nào để bạn tìm thấy khoảng cách giữa hai chuỗi?

Ngược lại, bạn đã thực hiện một công việc rất tốt để đưa ra một giải pháp. Có nhiều cách để cải thiện nó mặc dù.

Như bạn lưu ý, đây chỉ là vấn đề tiếp theo chung dài nhất trong một ngụy trang mỏng. "Khoảng cách xóa" giữa hai chuỗi chỉ là tổng chiều dài của các chuỗi trừ chiều dài của LCS.

Đó là, LCS của

function deletionDistance[$str1, $str2] {
  // your code goes here
}

0 [4 ký tự] và
function deletionDistance[$str1, $str2] {
  // your code goes here
}

1 [5 ký tự] là
function deletionDistance[$str1, $str2] {
  // your code goes here
}

2 [3 ký tự], do đó khoảng cách xóa là [4 + 5] - 2 * 3 = 3.

Do đó, tất cả những gì bạn cần làm để giải quyết vấn đề là có được độ dài của LCS, vì vậy hãy giải quyết vấn đề đó.

Giải pháp của bạn khá tốt nhưng vấn đề chính là thời gian và bộ nhớ của O [Mn] nếu các chuỗi có độ dài m và n. Bạn có thể cải thiện điều này.

Điều đầu tiên cần chú ý là nếu các chuỗi có tiền tố hoặc hậu tố chung thì bạn có thể tự động loại bỏ nó. Đó là, khoảng cách xóa cho

function deletionDistance[$str1, $str2] {
  // your code goes here
}

3 và
function deletionDistance[$str1, $str2] {
  // your code goes here
}

4 giống như khoảng cách xóa cho
function deletionDistance[$str1, $str2] {
  // your code goes here
}

5 và
function deletionDistance[$str1, $str2] {
  // your code goes here
}

6. Nó rất rẻ và dễ dàng để xác định xem hai chuỗi có tiền tố và hậu tố chung hay không, và bạn đi từ việc có một mảng với 25*29 phần tử đến một mảng với các yếu tố 5*9, một chiến thắng lớn.It is very cheap and easy to determine if two strings have a common prefix and suffix, and you go from having an array with 25*29 elements to an array with 5*9 elements, a huge win.

Điều tiếp theo cần chú ý là: Bạn xây dựng toàn bộ mảng

function deletionDistance[$str1, $str2] {
  // your code goes here
}

7 phía trước, nhưng trong khi bạn đang điền vào mảng,
function deletionDistance[$str1, $str2] {
  // your code goes here
}

8 chỉ nhìn vào
function deletionDistance[$str1, $str2] {
  // your code goes here
}

9 hoặc
function deletionDistance[str1, str2]:
    str1Len = str1.length
    str2Len = str2.length
    
    # allocate a 2D array with str1Len + 1 rows and str2Len + 1 columns
    memo = new Array[str1Len + 1, str2Len + 1]

    for i from 0 to str1Len:
        for j from 0 to str2Len:
            if [i == 0]:
                memo[i][j] = j
            else if [j == 0]:
                memo[i][j] = i
            else if [str1[i-1] == str2[j-1]]:
                memo[i][j] = memo[i-1][j-1]
            else:
                memo[i][j] = 1 + min[memo[i-1][j], memo[i][j-1]]

    return memo[str1Len][str2Len]
0 hoặc
function deletionDistance[str1, str2]:
    str1Len = str1.length
    str2Len = str2.length
    
    # allocate a 2D array with str1Len + 1 rows and str2Len + 1 columns
    memo = new Array[str1Len + 1, str2Len + 1]

    for i from 0 to str1Len:
        for j from 0 to str2Len:
            if [i == 0]:
                memo[i][j] = j
            else if [j == 0]:
                memo[i][j] = i
            else if [str1[i-1] == str2[j-1]]:
                memo[i][j] = memo[i-1][j-1]
            else:
                memo[i][j] = 1 + min[memo[i-1][j], memo[i][j-1]]

    return memo[str1Len][str2Len]
1. Vì bạn không bao giờ nhìn vào một dòng mảng cách đó hai, bạn không bao giờ cần nhiều hơn hai dòng! Đó là, bạn có thể:

  • Phân bổ và tính toán dòng đầu tiên
  • Phân bổ và tính toán dòng thứ hai được đưa ra dòng đầu tiên
  • vứt bỏ dòng đầu tiên; Chúng tôi sẽ không bao giờ sử dụng nó nữa
  • Phân bổ và tính toán dòng thứ ba từ dòng thứ hai
  • vứt bỏ dòng thứ hai
  • … và như thế

Bạn vẫn thực hiện các hoạt động O [MN] và bạn vẫn phân bổ tổng số lượng bộ nhớ, nhưng bạn chỉ có một lượng nhỏ trong bộ nhớ cùng một lúc. Nếu các chuỗi lớn, đó là một khoản tiết kiệm đáng kể.

25 tháng 4 năm 2018

Tổng quan

Khoảng cách xóa của hai chuỗi là số lượng ký tự tối thiểu bạn cần xóa trong hai chuỗi để có được cùng một chuỗi. Chẳng hạn, khoảng cách xóa giữa

function deletionDistance[str1, str2]:
    str1Len = str1.length
    str2Len = str2.length
    
    # allocate a 2D array with str1Len + 1 rows and str2Len + 1 columns
    memo = new Array[str1Len + 1, str2Len + 1]

    for i from 0 to str1Len:
        for j from 0 to str2Len:
            if [i == 0]:
                memo[i][j] = j
            else if [j == 0]:
                memo[i][j] = i
            else if [str1[i-1] == str2[j-1]]:
                memo[i][j] = memo[i-1][j-1]
            else:
                memo[i][j] = 1 + min[memo[i-1][j], memo[i][j-1]]

    return memo[str1Len][str2Len]
2 và
function deletionDistance[str1, str2]:
    str1Len = str1.length
    str2Len = str2.length
    
    # allocate a 2D array with str1Len + 1 rows and str2Len + 1 columns
    memo = new Array[str1Len + 1, str2Len + 1]

    for i from 0 to str1Len:
        for j from 0 to str2Len:
            if [i == 0]:
                memo[i][j] = j
            else if [j == 0]:
                memo[i][j] = i
            else if [str1[i-1] == str2[j-1]]:
                memo[i][j] = memo[i-1][j-1]
            else:
                memo[i][j] = 1 + min[memo[i-1][j], memo[i][j-1]]

    return memo[str1Len][str2Len]
3 là
function deletionDistance[str1, str2]:
    str1Len = str1.length
    str2Len = str2.length
    
    # allocate a 2D array with str1Len + 1 rows and str2Len + 1 columns
    memo = new Array[str1Len + 1, str2Len + 1]

    for i from 0 to str1Len:
        for j from 0 to str2Len:
            if [i == 0]:
                memo[i][j] = j
            else if [j == 0]:
                memo[i][j] = i
            else if [str1[i-1] == str2[j-1]]:
                memo[i][j] = memo[i-1][j-1]
            else:
                memo[i][j] = 1 + min[memo[i-1][j], memo[i][j-1]]

    return memo[str1Len][str2Len]
4:

  • Bằng cách xóa
    function deletionDistance[str1, str2]:
        str1Len = str1.length
        str2Len = str2.length
        
        # allocate a 2D array with str1Len + 1 rows and str2Len + 1 columns
        memo = new Array[str1Len + 1, str2Len + 1]
    
        for i from 0 to str1Len:
            for j from 0 to str2Len:
                if [i == 0]:
                    memo[i][j] = j
                else if [j == 0]:
                    memo[i][j] = i
                else if [str1[i-1] == str2[j-1]]:
                    memo[i][j] = memo[i-1][j-1]
                else:
                    memo[i][j] = 1 + min[memo[i-1][j], memo[i][j-1]]
    
        return memo[str1Len][str2Len]
    
    5 và
    function deletionDistance[str1, str2]:
        str1Len = str1.length
        str2Len = str2.length
        
        # allocate a 2D array with str1Len + 1 rows and str2Len + 1 columns
        memo = new Array[str1Len + 1, str2Len + 1]
    
        for i from 0 to str1Len:
            for j from 0 to str2Len:
                if [i == 0]:
                    memo[i][j] = j
                else if [j == 0]:
                    memo[i][j] = i
                else if [str1[i-1] == str2[j-1]]:
                    memo[i][j] = memo[i-1][j-1]
                else:
                    memo[i][j] = 1 + min[memo[i-1][j], memo[i][j-1]]
    
        return memo[str1Len][str2Len]
    
    6 trong
    function deletionDistance[str1, str2]:
        str1Len = str1.length
        str2Len = str2.length
        
        # allocate a 2D array with str1Len + 1 rows and str2Len + 1 columns
        memo = new Array[str1Len + 1, str2Len + 1]
    
        for i from 0 to str1Len:
            for j from 0 to str2Len:
                if [i == 0]:
                    memo[i][j] = j
                else if [j == 0]:
                    memo[i][j] = i
                else if [str1[i-1] == str2[j-1]]:
                    memo[i][j] = memo[i-1][j-1]
                else:
                    memo[i][j] = 1 + min[memo[i-1][j], memo[i][j-1]]
    
        return memo[str1Len][str2Len]
    
    2 và
    function deletionDistance[str1, str2]:
        str1Len = str1.length
        str2Len = str2.length
        
        # allocate a 2D array with str1Len + 1 rows and str2Len + 1 columns
        memo = new Array[str1Len + 1, str2Len + 1]
    
        for i from 0 to str1Len:
            for j from 0 to str2Len:
                if [i == 0]:
                    memo[i][j] = j
                else if [j == 0]:
                    memo[i][j] = i
                else if [str1[i-1] == str2[j-1]]:
                    memo[i][j] = memo[i-1][j-1]
                else:
                    memo[i][j] = 1 + min[memo[i-1][j], memo[i][j-1]]
    
        return memo[str1Len][str2Len]
    
    8 trong
    function deletionDistance[str1, str2]:
        str1Len = str1.length
        str2Len = str2.length
        
        # allocate a 2D array with str1Len + 1 rows and str2Len + 1 columns
        memo = new Array[str1Len + 1, str2Len + 1]
    
        for i from 0 to str1Len:
            for j from 0 to str2Len:
                if [i == 0]:
                    memo[i][j] = j
                else if [j == 0]:
                    memo[i][j] = i
                else if [str1[i-1] == str2[j-1]]:
                    memo[i][j] = memo[i-1][j-1]
                else:
                    memo[i][j] = 1 + min[memo[i-1][j], memo[i][j-1]]
    
        return memo[str1Len][str2Len]
    
    3, chúng tôi nhận được chuỗi
    function deletionDistance[str1, str2]:
        # make sure the length of str2 isn't
        # longer than the length of str1
        if [str1.length < str2.length]
            tmpStr = str1
            str1 = str2
            str2 = tmpStr
    
        str1Len = str1.length
        str2Len = str2.length
        prevMemo = new Array[str2Len  + 1]
        currMemo = new Array[str2Len  + 1]
    
        for i from 0 to str1Len:
            for j from 0 to str2Len:
                if [i == 0]:
                    currMemo[j] = j
                else if [j == 0]:
                    currMemo[j] = i
                else if [str1[i-1] == str2[j-1]]:
                    currMemo[j] = prevMemo[j-1]
                else:
                    currMemo[j] = 1 + min[prevMemo[j], currMemo[j-1]]
    
            prevMemo = currMemo
            currMemo = new Array[str2Len + 1];  
                                               
        return prevMemo[str2Len]
    
    0 trong cả hai trường hợp.
  • Chúng ta không thể nhận được cùng một chuỗi từ cả hai chuỗi bằng cách xóa
    function deletionDistance[str1, str2]:
        # make sure the length of str2 isn't
        # longer than the length of str1
        if [str1.length < str2.length]
            tmpStr = str1
            str1 = str2
            str2 = tmpStr
    
        str1Len = str1.length
        str2Len = str2.length
        prevMemo = new Array[str2Len  + 1]
        currMemo = new Array[str2Len  + 1]
    
        for i from 0 to str1Len:
            for j from 0 to str2Len:
                if [i == 0]:
                    currMemo[j] = j
                else if [j == 0]:
                    currMemo[j] = i
                else if [str1[i-1] == str2[j-1]]:
                    currMemo[j] = prevMemo[j-1]
                else:
                    currMemo[j] = 1 + min[prevMemo[j], currMemo[j-1]]
    
            prevMemo = currMemo
            currMemo = new Array[str2Len + 1];  
                                               
        return prevMemo[str2Len]
    
    1 chữ cái hoặc ít hơn. Với các chuỗi
    function deletionDistance[str1, str2]:
        # make sure the length of str2 isn't
        # longer than the length of str1
        if [str1.length < str2.length]
            tmpStr = str1
            str1 = str2
            str2 = tmpStr
    
        str1Len = str1.length
        str2Len = str2.length
        prevMemo = new Array[str2Len  + 1]
        currMemo = new Array[str2Len  + 1]
    
        for i from 0 to str1Len:
            for j from 0 to str2Len:
                if [i == 0]:
                    currMemo[j] = j
                else if [j == 0]:
                    currMemo[j] = i
                else if [str1[i-1] == str2[j-1]]:
                    currMemo[j] = prevMemo[j-1]
                else:
                    currMemo[j] = 1 + min[prevMemo[j], currMemo[j-1]]
    
            prevMemo = currMemo
            currMemo = new Array[str2Len + 1];  
                                               
        return prevMemo[str2Len]
    
    2 và
    function deletionDistance[str1, str2]:
        # make sure the length of str2 isn't
        # longer than the length of str1
        if [str1.length < str2.length]
            tmpStr = str1
            str1 = str2
            str2 = tmpStr
    
        str1Len = str1.length
        str2Len = str2.length
        prevMemo = new Array[str2Len  + 1]
        currMemo = new Array[str2Len  + 1]
    
        for i from 0 to str1Len:
            for j from 0 to str2Len:
                if [i == 0]:
                    currMemo[j] = j
                else if [j == 0]:
                    currMemo[j] = i
                else if [str1[i-1] == str2[j-1]]:
                    currMemo[j] = prevMemo[j-1]
                else:
                    currMemo[j] = 1 + min[prevMemo[j], currMemo[j-1]]
    
            prevMemo = currMemo
            currMemo = new Array[str2Len + 1];  
                                               
        return prevMemo[str2Len]
    
    3, hãy viết một hàm hiệu quả
    function deletionDistance[str1, str2]:
        # make sure the length of str2 isn't
        # longer than the length of str1
        if [str1.length < str2.length]
            tmpStr = str1
            str1 = str2
            str2 = tmpStr
    
        str1Len = str1.length
        str2Len = str2.length
        prevMemo = new Array[str2Len  + 1]
        currMemo = new Array[str2Len  + 1]
    
        for i from 0 to str1Len:
            for j from 0 to str2Len:
                if [i == 0]:
                    currMemo[j] = j
                else if [j == 0]:
                    currMemo[j] = i
                else if [str1[i-1] == str2[j-1]]:
                    currMemo[j] = prevMemo[j-1]
                else:
                    currMemo[j] = 1 + min[prevMemo[j], currMemo[j-1]]
    
            prevMemo = currMemo
            currMemo = new Array[str2Len + 1];  
                                               
        return prevMemo[str2Len]
    
    4 trả về khoảng cách xóa giữa chúng. Giải thích cách hoạt động của chức năng của bạn và phân tích độ phức tạp về thời gian và không gian của nó.

Examples:

input:  str1 = "dog", str2 = "frog"
output: 3

input:  str1 = "some", str2 = "some"
output: 0

input:  str1 = "some", str2 = "thing"
output: 9

input:  str1 = "", str2 = ""
output: 0

Constraints:

  • [Đầu vào] Chuỗi Str1
  • [đầu vào] Chuỗi str2
  • [đầu ra] Số nguyên
function deletionDistance[$str1, $str2] {
  // your code goes here
}

Gợi ý

  • Đề xuất bạn bè của bạn để xác định các trường hợp cơ sở trước. Đó là, các trường hợp một trong các chuỗi là chuỗi trống. Trong trường hợp này, khoảng cách xóa chỉ đơn giản là độ dài của chuỗi khác. Sau đó, khuyến khích họ thử các trường hợp có phần giống nhau, chẳng hạn như một chuỗi chứa các ký tự
    function deletionDistance[str1, str2]:
        # make sure the length of str2 isn't
        # longer than the length of str1
        if [str1.length < str2.length]
            tmpStr = str1
            str1 = str2
            str2 = tmpStr
    
        str1Len = str1.length
        str2Len = str2.length
        prevMemo = new Array[str2Len  + 1]
        currMemo = new Array[str2Len  + 1]
    
        for i from 0 to str1Len:
            for j from 0 to str2Len:
                if [i == 0]:
                    currMemo[j] = j
                else if [j == 0]:
                    currMemo[j] = i
                else if [str1[i-1] == str2[j-1]]:
                    currMemo[j] = prevMemo[j-1]
                else:
                    currMemo[j] = 1 + min[prevMemo[j], currMemo[j-1]]
    
            prevMemo = currMemo
            currMemo = new Array[str2Len + 1];  
                                               
        return prevMemo[str2Len]
    
    5 hoặc
    function deletionDistance[str1, str2]:
        # make sure the length of str2 isn't
        # longer than the length of str1
        if [str1.length < str2.length]
            tmpStr = str1
            str1 = str2
            str2 = tmpStr
    
        str1Len = str1.length
        str2Len = str2.length
        prevMemo = new Array[str2Len  + 1]
        currMemo = new Array[str2Len  + 1]
    
        for i from 0 to str1Len:
            for j from 0 to str2Len:
                if [i == 0]:
                    currMemo[j] = j
                else if [j == 0]:
                    currMemo[j] = i
                else if [str1[i-1] == str2[j-1]]:
                    currMemo[j] = prevMemo[j-1]
                else:
                    currMemo[j] = 1 + min[prevMemo[j], currMemo[j-1]]
    
            prevMemo = currMemo
            currMemo = new Array[str2Len + 1];  
                                               
        return prevMemo[str2Len]
    
    1.
  • Nếu bạn bè cần hỗ trợ bổ sung, hãy giúp họ bằng cách gợi ý về mối quan hệ đệ quy giữa
    function deletionDistance[str1, str2]:
        # make sure the length of str2 isn't
        # longer than the length of str1
        if [str1.length < str2.length]
            tmpStr = str1
            str1 = str2
            str2 = tmpStr
    
        str1Len = str1.length
        str2Len = str2.length
        prevMemo = new Array[str2Len  + 1]
        currMemo = new Array[str2Len  + 1]
    
        for i from 0 to str1Len:
            for j from 0 to str2Len:
                if [i == 0]:
                    currMemo[j] = j
                else if [j == 0]:
                    currMemo[j] = i
                else if [str1[i-1] == str2[j-1]]:
                    currMemo[j] = prevMemo[j-1]
                else:
                    currMemo[j] = 1 + min[prevMemo[j], currMemo[j-1]]
    
            prevMemo = currMemo
            currMemo = new Array[str2Len + 1];  
                                               
        return prevMemo[str2Len]
    
    7 và
    function deletionDistance[str1, str2]:
        # make sure the length of str2 isn't
        # longer than the length of str1
        if [str1.length < str2.length]
            tmpStr = str1
            str1 = str2
            str2 = tmpStr
    
        str1Len = str1.length
        str2Len = str2.length
        prevMemo = new Array[str2Len  + 1]
        currMemo = new Array[str2Len  + 1]
    
        for i from 0 to str1Len:
            for j from 0 to str2Len:
                if [i == 0]:
                    currMemo[j] = j
                else if [j == 0]:
                    currMemo[j] = i
                else if [str1[i-1] == str2[j-1]]:
                    currMemo[j] = prevMemo[j-1]
                else:
                    currMemo[j] = 1 + min[prevMemo[j], currMemo[j-1]]
    
            prevMemo = currMemo
            currMemo = new Array[str2Len + 1];  
                                               
        return prevMemo[str2Len]
    
    4 cho một số tiền tố của
    function deletionDistance[str1, str2]:
        # make sure the length of str2 isn't
        # longer than the length of str1
        if [str1.length < str2.length]
            tmpStr = str1
            str1 = str2
            str2 = tmpStr
    
        str1Len = str1.length
        str2Len = str2.length
        prevMemo = new Array[str2Len  + 1]
        currMemo = new Array[str2Len  + 1]
    
        for i from 0 to str1Len:
            for j from 0 to str2Len:
                if [i == 0]:
                    currMemo[j] = j
                else if [j == 0]:
                    currMemo[j] = i
                else if [str1[i-1] == str2[j-1]]:
                    currMemo[j] = prevMemo[j-1]
                else:
                    currMemo[j] = 1 + min[prevMemo[j], currMemo[j-1]]
    
            prevMemo = currMemo
            currMemo = new Array[str2Len + 1];  
                                               
        return prevMemo[str2Len]
    
    2 và
    function deletionDistance[str1, str2]:
        # make sure the length of str2 isn't
        # longer than the length of str1
        if [str1.length < str2.length]
            tmpStr = str1
            str1 = str2
            str2 = tmpStr
    
        str1Len = str1.length
        str2Len = str2.length
        prevMemo = new Array[str2Len  + 1]
        currMemo = new Array[str2Len  + 1]
    
        for i from 0 to str1Len:
            for j from 0 to str2Len:
                if [i == 0]:
                    currMemo[j] = j
                else if [j == 0]:
                    currMemo[j] = i
                else if [str1[i-1] == str2[j-1]]:
                    currMemo[j] = prevMemo[j-1]
                else:
                    currMemo[j] = 1 + min[prevMemo[j], currMemo[j-1]]
    
            prevMemo = currMemo
            currMemo = new Array[str2Len + 1];  
                                               
        return prevMemo[str2Len]
    
    3. Sau khi họ tìm thấy mối quan hệ, bạn có thể đề nghị sử dụng lập trình động.
  • Nếu bạn bè của bạn vẫn bị mắc kẹt khi tìm kiếm mối quan hệ đệ quy, hướng dẫn họ xem xét hai trường hợp:
    • Trường hợp 1: Nhân vật cuối cùng trong
      function deletionDistance[str1, str2]:
          # make sure the length of str2 isn't
          # longer than the length of str1
          if [str1.length < str2.length]
              tmpStr = str1
              str1 = str2
              str2 = tmpStr
      
          str1Len = str1.length
          str2Len = str2.length
          prevMemo = new Array[str2Len  + 1]
          currMemo = new Array[str2Len  + 1]
      
          for i from 0 to str1Len:
              for j from 0 to str2Len:
                  if [i == 0]:
                      currMemo[j] = j
                  else if [j == 0]:
                      currMemo[j] = i
                  else if [str1[i-1] == str2[j-1]]:
                      currMemo[j] = prevMemo[j-1]
                  else:
                      currMemo[j] = 1 + min[prevMemo[j], currMemo[j-1]]
      
              prevMemo = currMemo
              currMemo = new Array[str2Len + 1];  
                                                 
          return prevMemo[str2Len]
      
      2 bằng với ký tự cuối cùng trong
      function deletionDistance[str1, str2]:
          # make sure the length of str2 isn't
          # longer than the length of str1
          if [str1.length < str2.length]
              tmpStr = str1
              str1 = str2
              str2 = tmpStr
      
          str1Len = str1.length
          str2Len = str2.length
          prevMemo = new Array[str2Len  + 1]
          currMemo = new Array[str2Len  + 1]
      
          for i from 0 to str1Len:
              for j from 0 to str2Len:
                  if [i == 0]:
                      currMemo[j] = j
                  else if [j == 0]:
                      currMemo[j] = i
                  else if [str1[i-1] == str2[j-1]]:
                      currMemo[j] = prevMemo[j-1]
                  else:
                      currMemo[j] = 1 + min[prevMemo[j], currMemo[j-1]]
      
              prevMemo = currMemo
              currMemo = new Array[str2Len + 1];  
                                                 
          return prevMemo[str2Len]
      
      3. Trong trường hợp đó, người ta có thể cho rằng hai nhân vật này đã bị xóa và nhìn vào các tiền tố mà don lồng bao gồm nhân vật cuối cùng.
    • Trường hợp 2: Nhân vật cuối cùng trong
      function deletionDistance[str1, str2]:
          # make sure the length of str2 isn't
          # longer than the length of str1
          if [str1.length < str2.length]
              tmpStr = str1
              str1 = str2
              str2 = tmpStr
      
          str1Len = str1.length
          str2Len = str2.length
          prevMemo = new Array[str2Len  + 1]
          currMemo = new Array[str2Len  + 1]
      
          for i from 0 to str1Len:
              for j from 0 to str2Len:
                  if [i == 0]:
                      currMemo[j] = j
                  else if [j == 0]:
                      currMemo[j] = i
                  else if [str1[i-1] == str2[j-1]]:
                      currMemo[j] = prevMemo[j-1]
                  else:
                      currMemo[j] = 1 + min[prevMemo[j], currMemo[j-1]]
      
              prevMemo = currMemo
              currMemo = new Array[str2Len + 1];  
                                                 
          return prevMemo[str2Len]
      
      2 khác với ký tự cuối cùng trong
      function deletionDistance[str1, str2]:
          # make sure the length of str2 isn't
          # longer than the length of str1
          if [str1.length < str2.length]
              tmpStr = str1
              str1 = str2
              str2 = tmpStr
      
          str1Len = str1.length
          str2Len = str2.length
          prevMemo = new Array[str2Len  + 1]
          currMemo = new Array[str2Len  + 1]
      
          for i from 0 to str1Len:
              for j from 0 to str2Len:
                  if [i == 0]:
                      currMemo[j] = j
                  else if [j == 0]:
                      currMemo[j] = i
                  else if [str1[i-1] == str2[j-1]]:
                      currMemo[j] = prevMemo[j-1]
                  else:
                      currMemo[j] = 1 + min[prevMemo[j], currMemo[j-1]]
      
              prevMemo = currMemo
              currMemo = new Array[str2Len + 1];  
                                                 
          return prevMemo[str2Len]
      
      3. Trong trường hợp đó, ít nhất một trong các ký tự phải bị xóa, do đó chúng ta có được phương trình sau:
      function deletionDistance[$str1, $str2] {
      	$cur = range[0, strlen[$str1]];
      	$prev = [];
      	for[$i=0; $i$case] {
      	$index = $key+1;
      	if [$case[2] != deletionDistance[$case[0], $case[1]]] {
      		echo "Test case #{$index} failed
      \n"; } else { echo "Test case #{$index} passed
      \n"; } }
      7, vì chúng tôi vẫn chạy một vòng lặp lồng nhau với các lần lặp
      function deletionDistance[$str1, $str2] {
        // your code goes here
      }
      
      
      09.: the time complexity stays the same, i.e.
      $testCase = [
      	["", "", 0],
      	["", "hit", 3],
      	["neat", "", 4],
      	["heat", "hit", 3],
      	["hot", "not", 2],
      	["some", "thing", 9],
      	["abc", "adbc", 1],
      	["awesome", "awesome", 0],
      	["ab", "ba", 2],
      ];
      
      foreach[$testCase as $key=>$case] {
      	$index = $key+1;
      	if [$case[2] != deletionDistance[$case[0], $case[1]]] {
      		echo "Test case #{$index} failed
      \n"; } else { echo "Test case #{$index} passed
      \n"; } }
      7, since we still run a nested loop with
      function deletionDistance[$str1, $str2] {
        // your code goes here
      }
      
      
      09 iterations.

      Độ phức tạp không gian:

      function deletionDistance[$str1, $str2] {
        // your code goes here
      }
      
      
      10, vì chúng ta chỉ cần giữ hai hàng của mảng kép.:
      function deletionDistance[$str1, $str2] {
        // your code goes here
      }
      
      
      10, as we only need to hold two rows of the double array.

      Mã ví dụ

      Mã PHP

      function deletionDistance[$str1, $str2] {
      	$cur = range[0, strlen[$str1]];
      	$prev = [];
      	for[$i=0; $i

Bài Viết Liên Quan

Chủ Đề