[question 50] the first character that only appears once

Posted by clarket on Thu, 05 Dec 2019 13:32:44 +0100

[title]
Find the first character that appears only once in a string (0 < = string length < = 10000, all composed of letters) and return its position. If not, return - 1 (case sensitive)
[thinking]
You can count the number of occurrences of each character in a string,
1. Use two list s to record the characters that have appeared
[implementation]

import java.util.ArrayList;
import java.util.List;
public class Solution {
    public int FirstNotRepeatingChar(String str) {
        if(str.length() == 0 ||str == null){
            return -1;
        }
        List<Character> lista = new ArrayList<>();
        List<Character> listb = new ArrayList<>();
        for(int i =0;i<str.length();i++){
            char ch = str.charAt(i);//Get character at index i
            if(!lista.contains(ch) && !listb.contains(ch)){//If a and b do not contain the current character
                lista.add(Character.valueOf(ch));//Returns a character instance of the specified character value, added to a
            }else{
                lista.remove(Character.valueOf(ch));//Delete the current character in a
                listb.add(Character.valueOf(ch));//Add the character to b
            }
        }
        if(lista.size()<=0){//Deleted, indicating that there is no one time character
            return -1;
        }
        return str.indexOf(lista.get(0));
    }
}

[example]

2. Use hash table. The key value is character and the value is the number of times.
You need to scan the string twice from the beginning. When you scan the string for the first time, add 1 to the corresponding item in the hash table for each character you scan. When you scan for the second time, you can get the number of times the character appears in the hash table for each character you scan.
[implementation]

import java.util.LinkedHashMap;
public class Solution {
    public int FirstNotRepeatingChar(String str) {
        LinkedHashMap<Character,Integer> map = new LinkedHashMap<Character,Integer>();
        for(int i = 0;i<str.length();i++){
            if(map.containsKey(str.charAt(i))){//If the hash table contains the key as the current character
                int time = map.get(str.charAt(i));//Get the value of the current character
                map.put(str.charAt(i),++time);//Add this character to map, value+1
            }else{//If the hash table does not contain the current character, add it to the hash table
                map.put(str.charAt(i),1);
            }
        }
        int pos = -1;
        int i =0;
        for(;i<str.length();i++){
            char c = str.charAt(i);
            if(map.get(c)==1){
                return i;
            }
        }
        return pos;
    }
}

[example]

Reference resources:
1. Sword finger offer
2.https://blog.csdn.net/hsj1213522415/article/details/72763786
3.https://www.nowcoder.com/profile/844008/codeBookDetail?submissionId=1515918

Topics: Programming Java