python - Common substring of length k -
i'm trying write function gets 2 strings , integer 'k' , returns common substring of both strings of length k. (if there more 1, returns 1 @ random). there alot of algorithms online checks longest common substring found none checks k-length substring.
i think hash tables correct way if want optimized couldn't quite it.
i write function checks if there more 1 k-length sequence in list. here got:
def repeat(st, k): in range(len(st) - k + 1): j in range(i + 1, len(st) - k + 1): if st[i : + k] == st[j : j + k]: return st[i : + k] return false
i appreciate this... :/
simple version this:
def common_substr(a, b, k): substr in (a[i:i+k] in range(len(a)-k+1)): if substr in b: return substr
i guess large input strings (e. g. megabytes of text) , large k
might inefficient , building hashes of possible substrings of length k
can improve speed:
def common_substr(a, b, k): substrs = set(a[i:i+k] in range(len(a)-k+1)) substr in (b[i:i+k] in range(len(b)-k+1)): if substr in substrs: return substr
but guess there cleverer algorithms around this. comparably simple strstr()
(find string in string) has more efficient solutions straight-forward 1 can implement.
Comments
Post a Comment