class Solution: # @param start, a string # @param end, a string # @param dict, a set of string # @return an integer def ladderLength(self, start, end, dic): alph = [chr(c) for c in range(97, 123)] dic.add(end) n,m=len(dic),len(start) que=collections.deque([[start,1]]) while que: crt=que.popleft() crtword,crtlen=crt[0],crt[1] if crtword==end: return crtlen for i in range(m): s1=crtword[:i] s2=crtword[i+1:] for c in alph: nextword=s1+c+s2 if nextword in dic and c!=crtword[i]: dic.remove(nextword) que.append([nextword,crtlen+1]) return 0
Friday, September 19, 2014
Leetcode: Word Ladder @Python
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment