题解 | #查找兄弟单词#
查找兄弟单词
http://www.nowcoder.com/practice/03ba8aeeef73400ca7a37a5f3370fe68
package main
import (
"fmt"
"sort"
)
func main() {
count := 0
fmt.Scan(&count)
var strList []string
for count > 0 {
temp := ""
fmt.Scan(&temp)
strList = append(strList, temp)
count--
}
key := ""
fmt.Scan(&key)
index := 0
fmt.Scan(&index)
check(strList, key, index)
}
func check(strList []string, key string, index int) {
sort.Strings(strList)
bCount := 0
bList := []string{}
for _, item := range strList {
if isBrother(item, key, &bList) {
bCount++
}
}
fmt.Println(bCount)
if len(bList) >= index {
fmt.Println(bList[index-1])
}
}
func isBrother(str string, key string, bList *[]string) bool {
if len(str) != len(key) || str == key {
return false
}
list := make([]int, 127)
for _, r := range key {
list[int(r)] += 1
}
for _, r := range str {
list[int(r)] -= 1
}
for _, item := range list {
if item != 0 {
return false
}
}
*bList = append(*bList, str)
return true
}