Find the first character that appears only once in a string and return its position. If not, return -1 (requires case sensitivit

题: in a string (0<=string length) <=10000, all composed of letters, find the first character that appears only once and return its position. If not, it returns -1 (requires case sensitivity).

思维:Create data structure HashMap, You can record the number of occurrences of each character, then traverse the string, store the corresponding value in the HashMap, after traversing the string, the number of occurrences of the corresponding character is also stored, and then traverse the string again to find the HashMap If the corresponding number of times is 1, the corresponding position is returned. The concrete code implementation is as follows:

import java.util.HashMap;

public class Solution {     public int FirstNotRepeatingChar(String str) {         HashMap<Character,Integer> mp=new HashMap<>();         if(str==null){             return -1;         }         for(int i=0;i<str.length();i++){             if(mp.containsKey(str.charAt(i))){                 int value=mp.get(str.charAt(i));                 mp.put(str.charAt(i),value+1);             }else{                 mp.put(str.charAt(i),1);             }         }         for(int j=0;j<str.length();j++){             if(mp.get(str.charAt(j))==1){                 return j;             }         }         return -1;     } }