luoheng
10/7/2019 - 10:55 AM

findWords

func toLower(c byte) byte {
    if c >= 'a' {
        return c
    }
    return c - 'A' + 'a'
}

func findWords(words []string) []string {
    hash := [3]map[byte]bool{
        map[byte]bool{
        'q': true, 'w': true, 'e': true, 'r': true, 't': true, 'y': true, 'u': true,
        'i': true, 'o': true, 'p': true,
        },
        map[byte]bool{
        'a': true, 's': true, 'd': true, 'f': true, 'g': true, 'h': true, 'j': true,
        'k': true, 'l': true,
        },
        map[byte]bool{
        'z': true, 'x': true, 'c': true, 'v': true, 'b': true, 'n': true, 'm': true,
        },
    }
    w := make([]string, 0, len(words))
    for i := 0; i < len(words); i++ {
        for j := 0; j < 3; j++ {
            if hash[j][toLower(words[i][0])] {
                k := 0
                for k = 0; k < len(words[i]); k++ {
                    if !hash[j][toLower(words[i][k])] {
                        break
                    }
                }
                if k == len(words[i]) {
                    w = append(w, words[i])
                }
                break
            }
        }
    }
    return w
}