Longest string problem, python implementation
The purpose of this blog is to remind you that you must submit the written examination in time ?!!!!
In the last question of Alibaba's written test, I followed the python implementation. The Code is as follows. I don't know if it is correct. At that time, I had already passed several data records.
However, I did not hand it in. The place where I entered the code was empty and it was useless to make a mistake. Write it down first and study it in another day:
def largest_common(query, text, is_first_same): if (len(query) == 0 or len(text) == 0): return 0 else: a = query[0] b = text[0] if is_first_same: if (a == b): first_same_l = largest_common(query[1:], text[1:],True)+1 else: first_same_l = 0 first_diff_l = max(largest_common(query[1:], text[0:],False), largest_common(query[0:],text[1:],False)) else: if (a == b): first_same_l = largest_common(query[1:], text[1:],True) else: first_same_l = 0 first_diff_l = max(largest_common(query[1:], text[0:],False), largest_common(query[0:],text[1:],False)) if first_same_l > first_diff_l : return first_same_l else: return first_diff_l if __name__ =='__main__': a = "acbac" b = "accbaccbabb" print largest_common(a, b, True)