题解 | #查找兄弟单词#

查找兄弟单词

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
}

全部评论

相关推荐

点赞 评论 收藏
分享
Twilight_m...:经典我朋友XXXX起手,这是那种经典的不知道目前行情搁那儿胡编乱造瞎指导的中年人,不用理这种**
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
06-30 18:19
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务