GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — algorithms/string/one_edit_distance.py at master · RajaRamu/algorithms Expand file tree/ Copy pathone_edit_distance.pyMore file actions 35 lines (31 loc) · 831 Bytes / Copy pathone_edit_distance.pyFile metadata and controls35 lines (31 loc) · 831 Bytes 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 """ Given two strings S and T, determine if they are both one edit distance apart. """ def is_one_edit(s, t): """ :type s: str :type t: str :rtype: bool """ if len(s) > len(t): return is_one_edit(t, s) if len(t) - len(s) > 1 or t == s: return False for i in range(len(s)): if s[i] != t[i]: return s[i+1:] == t[i+1:] or s[i:] == t[i+1:] return True def is_one_edit2(s, t): l1, l2 = len(s), len(t) if l1 > l2: return is_one_edit2(t, s) if len(t) - len(s) > 1 or t == s: return False for i in range(len(s)): if s[i] != t[i]: if l1 == l2: s = s[:i]+t[i]+s[i+1:] # modify else: s = s[:i]+t[i]+s[i:] # insertion break return s == t or s == t[:-1] |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |