I hate this problem because I can't write decent code for it!


  • 0
    class Trie(object):
        def __init__(self):
            self.nodes = {}
    
    class WordDictionary(object):
        def __init__(self):
            self.root = Trie()
            self.s = 'abcdefghijklmnopqrstuvwxyz'
    
        def addWord(self, word):
            r = self.root
            for w in word:
                if w not in r.nodes:
                    r.nodes[w] = Trie()
                r = r.nodes[w]
            r.nodes['#'] = 1
    
        def search(self, word):
            return self.searchNode(self.root.nodes, word)
        def searchNode(self, node, str):
            for i in xrange(len(str)):
                w = str[i]
                if w != '.':
                    if w in node:
                        node = node[w].nodes
                    else:
                        return False
                else:
                    for c in self.s:
                        if c in node and self.searchNode(node[c].nodes, str[i+1:]):
                            return True
                    return False
            return '#' in node
    

  • 0
    C

    @Ipeq1 just curious what are decent codes?


  • 0

    @Chengcheng.Pei

    I believe there should always be some very concise or trenchant way to "throw hit" to the problem, which means less code, and more readability. Less code usually leads to low buggy and high maintainable code, I'm a fan of that!


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.