java-HJ59_找出字符串中第一个只出现一次的字符

java-HJ59 找出字符串中第一个只出现一次的字符

import java.util.HashMap;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        while (in.hasNext()){
            String input = in.nextLine();

            // map
            solution1(input);

            // String.indexOf() && String.lastIndexOf()
            // solution2(input);

            // String.replaceAll()
            // solution3(input);

            // bitmap: char[]
            // solution4(input);
        }
    }

    /**
     * map
     * @param input
     */
    private static void solution1(String input){

        HashMap<Character, Integer> charCountMap = new HashMap<>();
        for(int i=0; i<input.length(); i++){
            char aChar = input.charAt(i);
            charCountMap.put(aChar, charCountMap.getOrDefault(aChar,0)+1);
        }

        boolean found = false;
        for(char aChar: input.toCharArray()){
            Integer aCharCount = charCountMap.get(aChar);
            if(aCharCount == 1){
                System.out.println(aChar);
                found = true;
                break;
            }
        }

        if(!found){
            System.out.println(-1);
        }
    }


    /**
     * String.indexOf() && String.lastIndexOf()
     * @param input
     */
    private static void solution2(String input){

        boolean found = false;
        for(char aChar: input.toCharArray()){
            if(input.indexOf(aChar) == input.lastIndexOf(aChar)){
                System.out.println(aChar);
                found = true;
                break;
            }
        }

        if(!found){
            System.out.println(-1);
        }
    }


    /**
     * String.replaceAll()
     * @param input
     */
    private static void solution3(String input){

        int inputLen = input.length();

        boolean found = false;
        for(char aChar: input.toCharArray()){
            String replacedStr = input.replaceAll(String.valueOf(aChar), "");
            int replacedStrLen = replacedStr.length();
            if(inputLen-replacedStrLen == 1){
                System.out.println(aChar);
                found = true;
                break;
            }
        }

        if(!found){
            System.out.println(-1);
        }
    }


    /**
     * bitmap: char[]
     * @param input
     */
    private static void solution4(String input){

        boolean found = false;
        char[] charsCount = new char[126];
        for(char aChar: input.toCharArray()){
            charsCount[aChar-'0']++;
        }

        for(char aChar: input.toCharArray()){
            if(charsCount[aChar-'0'] == 1){
                System.out.println(aChar);
                found = true;
                break;
            }
        }

        if(!found){
            System.out.println(-1);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值