题解 | #查找兄弟单词#

查找兄弟单词

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
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-03 18:22
投了几百份简历,专业和方向完全对口,都已读不回。尝试改了一下学校,果然有奇效。
steelhead:这不是很正常嘛,BOSS好的是即便是你学院本可能都会和聊几句,牛客上学院本机会很少了
点赞 评论 收藏
分享
不要停下啊:大二打开牛客,你有机会开卷了,卷起来,去找课程学习,在牛客上看看大家面试笔试都需要会什么,岗位有什么需求就去学什么,努力的人就一定会有收获,这句话从来都经得起考验,像我现在大三了啥也不会,被迫强行考研,炼狱难度开局,啥也不会,找工作没希望了,考研有丝丝机会
点赞 评论 收藏
分享
05-29 22:11
门头沟学院 Java
Elastic90:抛开学历造假不谈,这公司的招聘需求也挺怪的,Java开发还要求你有图文识别、移动端开发和c++的经验,有点逆天了。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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