题解 | #查找兄弟单词#
查找兄弟单词
https://www.nowcoder.com/practice/03ba8aeeef73400ca7a37a5f3370fe68
package main import ( "fmt" "sort" ) func isBrotherWord(s string, target string) bool { if s == target { return false } if len(s) != len(target) { return false } arr := make([]int, 128) for i:=0; i<len(target); i++ { arr[target[i]]++ } for i:=0; i<len(s); i++ { arr[s[i]]-- } for i:=0; i<len(arr); i++ { if arr[i] != 0 { return false } } return true } func findBrotherWords(words []string, target string) []string { var broWords []string for _, word := range words { if isBrotherWord(word, target) { broWords = append(broWords, word) } } return broWords } func main() { var m int fmt.Scan(&m) var words []string for i:=0; i<m; i++ { var word string fmt.Scan(&word) words = append(words, word) } var target string fmt.Scan(&target) var k int fmt.Scan(&k) broWords := findBrotherWords(words, target) sort.Strings(broWords) fmt.Println(len(broWords)) if len(broWords) > k { fmt.Println(broWords[k-1]) } }
// 本题输入为一行,包括数字和字符串,用空格分开,所以采用 fmt.Scan(&word)