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

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

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

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息//-----》不能使用HashMap 坑爹玩意儿
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        String str = in.nextLine();
        deleteWordLess(str);

    }

    public static void deleteWordLess(String input) {//aabbcaabb
        int[] positions ;
        int[] nums = new int[input.length()];
        int pos = 0;
        String targetStr = "";
        for (int i = 0; i < input.length(); i++) {
            nums[pos++] = input.charAt(i);
//          System.out.println(nums[pos-1]);//转化为整数数组

        }
        mySort(nums);//对整数数组排序
        int[] times = new int[getKind(nums)];//统计重复的次数
        int tops = 0;
        int count = 1;
        for (int k = 0; k < nums.length - 1; k++) {
            int current = k;
            int next = current + 1;
            if (nums[current] == nums[next]) { //aaaabbbbb
                ++count;
            } else {
                times[tops++] = count;
                count = 1;
            }

            if (next == nums.length - 1) {
                times[tops++] = count;
            }
        }
        positions = minPosition(times);//获取最小的次数的位置集合

        for (int a : positions) {
            char tragetChar = (char) nums[a];
//          System.out.println("tragetChar:"+tragetChar);
            targetStr = DeleteChar(input, tragetChar);
            input = targetStr;

        }


        System.out.println(targetStr);

    }
    public static String DeleteChar(String str, char target) {

        return str.replaceAll(String.valueOf(target), "");
    }


    public static void mySort(int[] arry) {

        for (int i = arry.length - 1; i > 0; i--) {
            for (int k = 0; k < i; k++) {
                if (arry[k] > arry[i])
                    swap(arry, i, k);
            }
        }
    }

    public static int getKind(int[] arry) {//找到种类数目
        int count = 1;
        for (int k = 0; k < arry.length - 1; k++) {
            int current = k;
            int next = current + 1;
            if (arry[current] != arry[next])
                count++;
        }
        return count;
    }

//    public static String test1= "aabcddd";//
    //查找到最小的数的地址
    public static int[] minPosition(int[]
                                    times) {//通过种类最小的找到最小的次数的位置
        int minNum = 0;
        int position = 0;
        int pos = 0;
        for (int i = 0; i < times.length - 1; i++) {
            minNum = min(times[i], times[i + 1]);
        }
        int[] postions = new int[theSameSize(times, minNum)];
        for (int k = 0; k < times.length; k++) {
            if (times[k] == minNum)
                postions[pos++] = position;
            position += times[k];
        }

//      for(int a:postions) {
//          System.out.println("postion:"+a);
//      }

        return postions;
    }





    public static int theSameSize(int[] times,
                                  int min) { //找到同样次数的字母的种类
        int size = 0;
        for (int i = 0; i < times.length; i++) {
            if (times[i] == min)
                size++;
        }
        return size;

    }

    public static int min(int a, int b) { //判断最小数

        return a < b ? a : b;

    }

    public static void swap(int[]array, int a, int b) {

        int temp = array[a];
        array[a] = array[b];
        array[b] = temp;

    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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