Skip to content

Commit

Permalink
Add some comments in code
Browse files Browse the repository at this point in the history
  • Loading branch information
BlasterAlex committed Sep 3, 2024
1 parent 9adaca2 commit ce6560f
Showing 1 changed file with 38 additions and 23 deletions.
61 changes: 38 additions & 23 deletions solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class Solution:
wordQueue = queue.Queue()

def prepareData(self, beginWord: str, endWord: str, wordList: List[str]):
"""Required data initialization"""

self.positionLetters = []
self.wordNeighborsCache = {}
self.wordSet = set(wordList)
Expand All @@ -60,6 +62,8 @@ def prepareData(self, beginWord: str, endWord: str, wordList: List[str]):
self.wordQueue.put(QueuedWord(w, False))

def findWordNeighbors(self, word: str) -> Set[str]:
"""Getting a list of all adjacent words with result caching"""

if word in self.wordNeighborsCache:
return self.wordNeighborsCache[word]

Expand All @@ -76,17 +80,44 @@ def findWordNeighbors(self, word: str) -> Set[str]:
return neighbors

def wordPathFound(self, qWord: QueuedWord) -> bool:
"""Checking the condition for finding a new path intersection"""

if qWord.forward:
return qWord.word in self.backwardWordTree
else:
return qWord.word in self.forwardWordTree

def wordProcessing(self, qWord: QueuedWord):
"""Processing word from word queue"""

word = qWord.word
forward = qWord.forward
neighborLevel = qWord.level + 1

for neighbor in self.findWordNeighbors(word):
wordTree = self.forwardWordTree if forward else self.backwardWordTree
if word in wordTree:
if neighbor in wordTree[word].children:
continue

if neighbor in wordTree:
node = wordTree[neighbor]
if node.level == neighborLevel:
node.children.add(word)
else:
wordTree[neighbor] = WordTreeNode({word}, neighborLevel)
self.wordQueue.put(QueuedWord(neighbor, forward, neighborLevel))

def buildCrossPaths(self, point: str) -> List[List[str]]:
"""Getting a list of all word paths that intersect at the given point"""

forwardPaths = self.buildWordPaths(point, True)
backwardPaths = self.buildWordPaths(point, False)
return [forwardPath + backwardPath[1:] for forwardPath in forwardPaths for backwardPath in backwardPaths]

def buildWordPaths(self, word: str, forward: bool) -> List[List[str]]:
"""Getting a list of all word path """

wordTree = self.forwardWordTree if forward else self.backwardWordTree
if word not in wordTree:
return [[word]]
Expand All @@ -103,25 +134,6 @@ def buildWordPaths(self, word: str, forward: bool) -> List[List[str]]:

return result

def wordProcessing(self, qWord: QueuedWord):
word = qWord.word
forward = qWord.forward
neighborLevel = qWord.level + 1

for neighbor in self.findWordNeighbors(word):
wordTree = self.forwardWordTree if forward else self.backwardWordTree
if word in wordTree:
if neighbor in wordTree[word].children:
continue

if neighbor in wordTree:
node = wordTree[neighbor]
if node.level == neighborLevel:
node.children.add(word)
else:
wordTree[neighbor] = WordTreeNode({word}, neighborLevel)
self.wordQueue.put(QueuedWord(neighbor, forward, neighborLevel))

def findLadders(self, beginWord: str, endWord: str, wordList: List[str]) -> List[List[str]]:
if endWord not in wordList:
return []
Expand All @@ -133,21 +145,24 @@ def findLadders(self, beginWord: str, endWord: str, wordList: List[str]) -> List
self.prepareData(beginWord, endWord, wordList)

result = []
foundWords = set()
pathsFound = set()
foundLevel = 0
foundForward = False

# word queue processing
while not self.wordQueue.empty():
qWord = self.wordQueue.get()
if foundLevel > 0:
if qWord in foundWords or qWord.forward != foundForward or qWord.level > foundLevel:
# given path has already been found or all possible points of the same level have been processed
if qWord in pathsFound or qWord.forward != foundForward or qWord.level > foundLevel:
continue
if self.wordPathFound(qWord):
foundWords.add(qWord)
foundLevel, foundForward = qWord.level, qWord.forward
# word path intersection point is found, save the paths
result += self.buildCrossPaths(qWord.word)
foundLevel, foundForward = qWord.level, qWord.forward
pathsFound.add(qWord)
else:
# processing word from queue
self.wordProcessing(qWord)

return result

0 comments on commit ce6560f

Please sign in to comment.