题解 | 查找兄弟单词

查找兄弟单词

https://www.nowcoder.com/practice/03ba8aeeef73400ca7a37a5f3370fe68

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
	"strings"
)

// 获取输入内容
func getInputText() (count int, brother []string, words string, index int) {
    reader := bufio.NewReader(os.Stdin)
    inputText, _ := reader.ReadString('\n')
    inputText = strings.TrimSuffix(inputText, "\n")
    inputSlice := strings.Split(inputText, " ")

    // 字符串个数
    count, _ = strconv.Atoi(inputSlice[0])
    // 兄弟字符串
    brother = inputSlice[1:len(inputSlice) - 2]
    // 指定字符串
    words = inputSlice[len(inputSlice) - 2]
    // 指定兄弟单词的序号
    index, _ = strconv.Atoi(inputSlice[len(inputSlice) - 1])
    return
}

func main() {
    _, brother, words, index := getInputText()
    // fmt.Println(brother, words, index)
    res := make([]string, 0) // 符合条件字符串
    // 记录指定字符串每个字母出现的次数
    tagMap := make(map[string]int)
    for _, v := range words {
        tagMap[string(v)]++
    }

    for _, wordsItem := range brother {
        if len(string(wordsItem)) < 2 || len(string(wordsItem)) != len(words) || string(wordsItem) == words {
            continue
        }
        itemMap := make(map[string]int)
        for _, v := range string(wordsItem) {
            itemMap[string(v)]++
        }
        flag := 0
        for k, _ := range tagMap {// 两个map作比较
            if itemMap[string(k)] != tagMap[string(k)] {
                flag = 1
            }
        }
        if flag == 0 {
            res = append(res, string(wordsItem))
        }
    }

    sort.Strings(res)

    fmt.Println(len(res))
    if len(res) < index{
        return
    }
    fmt.Print(res[index-1])
}

全部评论

相关推荐

程序员花海:实习和校招简历正确格式应该是教育背景+实习+项目经历+个人评价 其中项目经历注意要体现业务 实习经历里面的业务更是要自圆其说 简历模板尽可能保持干净整洁 不要太花哨的
点赞 评论 收藏
分享
10-29 19:45
吉林大学 Java
从零开始数:自我评价没有必要写,但是看起来你应该是学了csdiy的一些课程,可以在专业技能里面写上自己比较熟悉操作系统和计网,但如果你是找Java的话,把第一个项目换了吧,现在看起来有点四不像。 无论是黑马点评或者说做个轮子项目,刷题和八股也搞起来吧,而且也没必要等到寒假,最近就可以开始找,找到就偷偷实习呗,别被逮到就行了。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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