Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's my rather anti-pythonic implementation that nevertheless leverages python's wonderful built in sets and strings.</p> <pre><code>a = 'ABCDDEGF' b = 'FPCDBDAX' best_solution = None best_solution_total_length = 0 def try_expand(a, b, a_loc, b_loc): # out of range checks if a_loc[0] &lt; 0 or b_loc[0] &lt; 0: return if a_loc[1] == len(a) or b_loc[1] == len(b): return if set(a[a_loc[0] : a_loc[1]]) == set(b[b_loc[0] : b_loc[1]]): global best_solution_total_length, best_solution #is this solution better than anything before it? if (len(a[a_loc[0] : a_loc[1]]) + len(b[b_loc[0] : b_loc[1]])) &gt; best_solution_total_length: best_solution = (a_loc, b_loc) best_solution_total_length = len(a[a_loc[0] : a_loc[1]]) + len(b[b_loc[0] : b_loc[1]]) try_expand(a, b, (a_loc[0]-1, a_loc[1]), (b_loc[0], b_loc[1])) try_expand(a, b, (a_loc[0], a_loc[1]+1), (b_loc[0], b_loc[1])) try_expand(a, b, (a_loc[0], a_loc[1]), (b_loc[0]-1, b_loc[1])) try_expand(a, b, (a_loc[0], a_loc[1]), (b_loc[0], b_loc[1]+1)) for a_i in range(len(a)): for b_i in range(len(b)): # starts of the recursive expansion from identical letters in two substrings if a[a_i] == b[b_i]: # if substrings were expanded from this range before then there won't be an answer there if best_solution == None or best_solution[0][0] &gt; a_i or best_solution[0][1] &lt;= a_i or best_solution[1][0] &gt; b_i or best_solution[1][1] &lt;= b_i: try_expand(a, b, (a_i, a_i), (b_i, b_i)) print a[best_solution[0][0] : best_solution[0][1]], b[best_solution[1][0] : best_solution[1][1]] </code></pre> <p>Forgot to mention that this is obviously a fairly bruteforce approach and I'm sure there's an algorithm that runs much, much faster.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload