题解 | #删除字符串中出现次数最少的字符#
删除字符串中出现次数最少的字符
https://www.nowcoder.com/practice/05182d328eb848dda7fdd5e029a56da9
package main import ( "fmt" ) func main() { var s string fmt.Scan(&s) // 统计字符出现的次数 counts := make(map[byte]int) for i := 0; i < len(s); i++ { counts[s[i]]++ } // 找出出现次数最少的字符 minCount := len(s) for _, count := range counts { if count < minCount { minCount = count } } // 删除出现次数最少的字符 result := "" for i := 0; i < len(s); i++ { if counts[s[i]] != minCount { result += string(s[i]) } } fmt.Println(result) }
package main import ( "fmt" ) func main() { var s string fmt.Scan(&s) // 统计字符出现的次数 counts := make(map[byte]int) for i := 0; i < len(s); i++ { counts[s[i]]++ } // 找出出现次数最少的字符 minCount := len(s) for _, count := range counts { if count < minCount { minCount = count } } // 删除出现次数最少的字符 result := "" for i := 0; i < len(s); i++ { if counts[s[i]] != minCount { result += string(s[i]) } } fmt.Println(result) }
华为机试题解 文章被收录于专栏
华为机试102道题解