计算最长子序列匹配度 | Get Longest Common Subsequence Matching Percentage

def GetLongestCommonSubsequenceMatchingPercentage(text: str, sub: str) -> int:
    if not text or not sub: return 0
    text = text.strip("\t\r\n ").lower()
    sub = sub.strip("\t\r\n ").lower()
    if (len(text) == 0) or (len(sub) == 0): return 0

    dp = [0 for _ in range(len(sub) + 1)]
    for i in range(1, len(text) + 1):
        pre = 0
        for j in range(1, len(sub) + 1):
            temp = dp[j]
            if text[i - 1] == sub[j - 1]:
                dp[j] = pre + 1
            else:
                dp[j] = max(dp[j], dp[j - 1])
            pre = temp
    return dp[-1] / len(text)
0 0 vote
Article Rating