题解 | #删除字符串中出现次数最少的字符#

删除字符串中出现次数最少的字符

http://www.nowcoder.com/practice/05182d328eb848dda7fdd5e029a56da9

import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); String str = sc.nextLine(); int[] temp = new int[26]; char[] chars = str.toCharArray(); for(int i = 0; i < chars.length;i++){ //统计各字母出现的次数 int index = chars[i] - 97; temp[index] += 1;

    }
     //对出现的次数进行排序
    int min = Integer.MAX_VALUE;
    for(int i=0; i < 26; i++){
        if(temp[i] != 0){
            min = Math.min(min,temp[i]);
        }
    }
        //删除出现次数最少的字母
    //如果有相同次数的字母,则一起删除
    for(int i =0; i<26;i++){
        if(temp[i] == min){
            char ch = (char)('a' + i);
            str = str.replace(ch + "","");
        }
    }
      System.out.println(str);  
}

}

全部评论

相关推荐

06-13 10:15
门头沟学院 Java
想去夏威夷的大西瓜在...:我也是27届,但是我现在研一下了啥项目都没有呀咋办,哎,简历不知道咋写
点赞 评论 收藏
分享
程序员饺子:正常 我沟通了200多个 15个要简历 面试2个 全投的成都的小厂。很多看我是27直接不会了😅
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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