GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — algorithms/string/one_edit_distance.py at master · RajaRamu/algorithms

Expand file tree

/

one_edit_distance.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
35 lines (31 loc) · 831 Bytes
/

one_edit_distance.py

Copy path

File metadata and controls

35 lines (31 loc) · 831 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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.