golang solution


  • 0
    G
    type ValidWordAbbr struct {
        M map[string]int
        dict map[string]int
    }
    
    
    func format(w string) string {
        if len(w) > 2 {
            return w[:1] + fmt.Sprintf("%v",len(w) - 2) + w[len(w) - 1:]
        }
        
        return w
    }
    
    func Constructor(dictionary []string) ValidWordAbbr {
    
        var abbr ValidWordAbbr
        abbr.M = make(map[string]int)
        abbr.dict = make(map[string]int)
        
        for _,w := range dictionary{
            abbr.M[format(w)] += 1
            abbr.dict[w] += 1
        }
        
        return abbr
    }
    
    
    func (this *ValidWordAbbr) IsUnique(word string) bool {
        abbr := format(word)
        if this.M[abbr] != this.dict[word]{
            return false
        }
        
        return true;
    }
    

Log in to reply
 

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