Finds the position of the first occurrence of a character in a string (1<= string length <=10000, all composed of letters). If the string is empty, return-1. The location index starts at 0.
If you start from scratch, make each character characters the character that follows it, and then end the program when the first occurrence of the characters story. But the time complexity of this algorithm is O (n^2);
Train of thought: we can iterate over the number of times each character appears in the string, save it in the array, you can use the hash algorithm, so that the ASCII code of characters and the value of the array to maintain a certain relationship, the value of the array to save the number of times, so that the formation of Key-value Depending on the ASCII value of the character, it can find its location in the array where the number of occurrences is saved.
The title is all composed of letters, the ASCII code of uppercase letters in 65-90, lowercase letters in 97-122, a total of 52 letters.
(1) Create an array of array size 52 to hold the number of occurrences of each letter;
(2) The relationship between the ASCII code value and the array index,
A (ASCII code 65) is held in the array at 0 positions, that is, the relationship between the ASCII code of uppercase letters (set to m) and its position in the array: m-65
A (ASCII code 97) is stored in an array of 26 digits, that is, the relationship between the ASCII code (set N) and the position in the array of lowercase write letters: n-71
(3) The first time to traverse the string, update the number of letters, each scan to a character in its array corresponding to the position of the number +1;
(4) Second traversal of the string, look up the number of letters, each scan to a character on the Finder in the corresponding position in the array of the value is 1, find the first, the size of the array index to return and exit the program.
public int Firstnotrepeatingchar (string str) {int index = -1;//If the string is null return-1 if (str!= null) {char [] Chararray = Str.tochararray ()//convert string to character array int[] count = new int[52];//Create an array of int-type length 52 to hold the number of occurrences of each letter//first traverse the string, update the letter occurrence Number (the position of the array according to the letter will be value +1) for (int i = 0; i < chararray.length; i++) {//If the letter is between 65-90, the position in the array is ASCII-65, and the number of +1 if
(chararray[i]>=65 && chararray[i]<=90)
{Count[chararray[i]-65]++; //If the letter is between 97-112, the position in the array is ASCII-71 and the number of times +1 if (chararray[i]>=97 && chararray[i]<=112) {count
[Chararray[i]-71]++;
}//The second traversal of the string to find the number of occurrences of the letter (how many times it has been saved in the count array according to the number of letters found) for (int i = 0; i < chararray.length i++) { If the letter is between 65-90, the position in the array is ASCII-65 and the number of times +1 if (chararray[i]>=65 && chararray[i]<=90) {if (Count[cha
Rarray[i]-(i) = = 1) {index = i;
Break }//If the letter is between 97-112, the position in the array is ASCII-71 and the number of times +1 if (chararray[i]>=97 && chararray[i]<=112) {if (Count[chararray[i]-$ = = 1) {index = i;
Break
}} return index; }