randythai1996
BAN USER
Comments (3)
Reputation 0
Page:
1
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
0
of 0 vote
def f(s, d):
h = set()
l = [s]
while l:
i = l.pop()
if len(i) == 1 and i in d:
return True
else:
for j in range(len(i)):
new = i[:j] + i[j+1:]
if new in d and new not in h:
l.append(new)
h.add(new)
return False
Comment hidden because of low score. Click to expand.
0
of 0 vote
from collections import deque
def f(s, t):
h = set([s])
s = [list(s), 0]
q = deque([s])
while q:
e, n = q.popleft()
if "".join(e) == t:
return n
else:
for i in range(len(e)):
for j in range(len(e)):
temp = e[i]
e[i] = e[j]
e[j] = temp
str = "".join(e)
if str not in h:
q.append([e[:], n+1])
h.add(str)
e[j] = e[i]
e[i] = temp
Page:
1
CareerCup is the world's biggest and best source for software engineering interview preparation. See all our resources.
Look up the Rabin-Karp Algorithm and Knuth-Morris-Pratt Algorithm for ways to optimize this.
- randythai1996 February 19, 2018