Golang solution (3ms)


  • 0
    func groupStrings(strings []string) [][]string {
        groupMap := make(map[string][]string)
        for i := 0; i < len(strings); i++ {
            s := key(strings[i])
            groupMap[s] = append(groupMap[s], strings[i])
        }
        result := make([][]string, len(groupMap))
        j := 0
        for _, list := range groupMap {
            result[j] = list
            j++
        }
        return result
    }
    
    func key(s string) string {
        const numChars int = 'z' - 'a' + 1
        keyStr := []byte{}
        for i := 1; i < len(s); i++ {
            diff := int(s[i]) - int(s[i - 1])
            if diff < 0 { diff += numChars }
            keyStr = append(keyStr, 'a' + byte(diff))
        }
        return string(keyStr)
    }
    

Log in to reply
 

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