Python 24/54 passed, I really don't understand some so call corner test cases !


  • 0
    W

    class ValidWordAbbr(object):

    def __init__(self, dictionary):
        """
        :type dictionary: List[str]
        """
            
        #print "__init__ dictionary = ", dictionary
        lenDic = len(dictionary)
        #print "lenDic = ", lenDic
        
        self.dic = {}
        self.oriDic = []
        
        for dic in dictionary:
            #print "dic = ", dic
            self.oriDic.append(dic)
            lenDic = len(dic)
            if lenDic > 3:
                abbr = dic[0] + str(len(dic[1:-1])) + dic[-1]
                #print "abbr = ", abbr
                
                if abbr not in self.dic:
                    self.dic[abbr] = 1
                else:
                    self.dic[abbr] += 1
            elif lenDic == 0:
                self.oriDic.append(dic)
    
    def isUnique(self, word):
        """
        :type word: str
        :rtype: bool
        """
        print "isUnique word = ", word, " self.dic = ", self.dic, "self.oriDic = ", self.oriDic
        lenWord = len(word)
        if lenWord == 0:
            return True
            
        if lenWord > 3:
            abbr = word[0] + str(len(word[1:-1])) + word[-1]
            print "abbr = ", abbr
            
            if word not in self.oriDic:
                if abbr in self.dic:
                    if self.dic[abbr] == 1:
                        print " ", word, " not in oriDic, so return True"
                        return False
                    else:
                        return False
                else:
                    return True
    
            if abbr in self.dic and self.dic[abbr] == 1:
                print "abbr = ", abbr, " return true"
                return True
                
        
        return False

Log in to reply
 

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