這裏是一個簡單的暴力。它計算每個可能的i和j對的長度。複雜度是O(n1.length * n2。長度* min(n1.length,n2.length))。當兩個字符串長度爲n時,這是O(n )。
for (i = 0; i < n1.length; i++)
{
for (j = 0; j < n2.length; j++)
{
errors = 0
len = 0
while (errors <= k && i+len < n1.length && j+len < n2.length)
{
if (n1[i+len] != n2[j+len]) errors++
if (errors <= k) len++
}
if (len > best) update best
}
}
下面是一個更有效的解決方案,通過i和j之間的所有可能的偏移量推移,設置有k個錯誤的子串在該偏移量,然後轉移沿子(與恰好有k誤差保持它)並觀察每個點的長度。複雜度爲O((n1.length + n2.length)* min(n1.length,n2.length))。當兩個字符串長度爲n時,這是O(n )。
for (offset = -n1.length; offset < n2.length; offset++)
{
// j is equal to (i + offset)
start = (offset > 0) ? 0 : -offset
stop = min(n1.length, n2.length - offset)
errors = 0
e = start // e is one past the end of the sequence, so l = e - i
for (i = start; i < stop; i++)
{
// move the end of the substring to maintain exactly k errors
while (e < stop && (errors < k || n1[e] == n2[e+offset]))
{
if (n1[e] != n2[e+offset]) errors++
e++
}
if (e - i > best) update best
if (n1[i] != n2[i+offset]) errors--
}
}
來源
2012-02-21 10:38:30
tom
如果我理解正確的話,在我看來是近似串匹配的變體(請參見:http://en.wikipedia.org/wiki/Approximate_string_matching)。 – 2012-02-18 08:52:44
可能是近似字符串匹配和查找最長公共子字符串的組合:http://en.wikipedia.org/wiki/Longest_common_substring_problem – 2012-02-19 05:09:50
來自hackerrank:https://www.hackerrank.com/challenges/substring-diff – 2014-04-03 22:08:33