Advertisement
Guest User

Untitled

a guest
Oct 19th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.33 KB | None | 0 0
  1. class Solution(object):
  2.     def findWords(self, board, words):
  3.         """
  4.        :type board: List[List[str]]
  5.        :type words: List[str]
  6.        :rtype: List[str]
  7.        """    
  8.        
  9.         class Trie(object):
  10.             def __init__(self):
  11.                 self.children = {}
  12.                
  13.             def insert(self, word):
  14.                 if word[0] not in self.children:
  15.                     self.children[word[0]] = Trie()
  16.                 l = len(word)
  17.                 if l == 1:
  18.                     return
  19.                 self.children[word[0]].__insert(word, 1, l)
  20.                
  21.             def __insert(self, word, i, l):
  22.                 if word[i] not in self.children:
  23.                     self.children[word[i]] = Trie()
  24.                 if l - i == 1:
  25.                     return
  26.                 self.children[word[i]].__insert(word, i+1, l)                
  27.            
  28.             def search(self, word):
  29.                 if word[0] not in self.children:
  30.                     return False
  31.                 l = len(word)
  32.                 if l == 1:
  33.                     return True
  34.                 return self.children[word[0]].__search(word, 1, l)
  35.            
  36.             def __search(self, word, i, l):
  37.                 if word[i] not in self.children:
  38.                     return False
  39.                 if l - i == 1:
  40.                     return True
  41.                 return self.children[word[i]].__search(word, i+1, l)
  42.            
  43.         from itertools import product
  44.         from collections import namedtuple
  45.         from copy import copy
  46.        
  47.         if type(board) is not list or len(board) == 0:
  48.             return []
  49.         if type(board[0]) is not list or len(board[0]) == 0:
  50.             return []
  51.         if type(words) is not list or len(words) == 0:
  52.             return []
  53.        
  54.         m = len(board)
  55.         n = len(board[0])
  56.        
  57.         trie = Trie()
  58.         words = [ list(w) for w in words ]
  59.        
  60.         for word in words:
  61.             trie.insert(word)
  62.            
  63.         Point = namedtuple('Point', ['x', 'y'])
  64.                
  65.        
  66.         def crawl(_letters, c_point, _points, output):
  67.             letters = copy(_letters)
  68.             x = c_point.x
  69.             y = c_point.y
  70.             letters.append(board[x][y])
  71.            
  72.             points = _points.copy()
  73.             points.add(c_point)
  74.            
  75.             if trie.search(letters):
  76.                 if letters in words:
  77.                     w = ''.join(letters)
  78.                     if w not in output:
  79.                         output.append(w)
  80.                    
  81.                
  82.                    
  83.                 # Make Points
  84.                 adjacent_points = []
  85.                 for p in [ Point(x-1,y), Point(x+1,y), Point(x,y-1), Point(x,y+1) ]:
  86.                     if (p not in points and
  87.                         m > p.x >= 0 and
  88.                         n > p.y >= 0):
  89.                             adjacent_points.append(p)
  90.                    
  91.                 for p in adjacent_points:
  92.                     crawl(letters, p, points, output)
  93.                    
  94.                 return False
  95.        
  96.         output = []
  97.         pointz = set()
  98.         for i,j in product(range(m), range(n)):
  99.             crawl([], Point(i,j), pointz, output)
  100.            
  101.         return output
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement