Find the position of the first character that appears only once

Source: Internet
Author: User
Tags lowercase


The topic describes finding the first occurrence of a character in a string (1<= string length <=10000, all composed of letters) and returning its position.

Analysis:

1, if empty string, or string length is greater than 10000 or less than 1, 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), which is obviously not a good solution.

Method: Using hash table data structure

1,

We can iterate over the number of occurrences of each character in a string, save it in an array (the hash table is implemented by an array), you can use the hash algorithm to keep the ASCII code of the character in relation to the key value of the array, that is, to calculate the hash code of each character (the index of the storage location in the array), and the value of the array , thus forming a key-value pair, which, depending on the ASCII value of the character, can find its place in the array where it appears.

2,

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.

[Java]public int Firstnotrepeatingchar (String str) {
int Len=str.length ();
if (str==null| | len<1| | len>10000)
return-1;
        int index =0;//returns -1   if the string is null              char [] charArray =  Str.tochararray ()//convert string to character array                 int[] count = new int[52];//creates an array of int type length 52 to hold the number of occurrences of each letter                                 //the first time the string is traversed, the number of occurrences of the letter is updated (the position of the array based on the letter will be +1)                 for  (int i = 0; i < len;  i++)                {                    //if the letters are between 65-90, The position in the array is ASCII-65 and the number of times is +1                   if ( chararray[i]>=65 && chararray[i]<=90)                     { 

int hash=ch[i]-65; Hash code for the first character
count[hash]++; //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) {int hash=ch[i]-71; Hash code for the first character
                    count[hash]++;                 }               }                                //the second traversal of the string to find the number of occurrences of the letter (number of occurrences of the number of letters saved in the Count array)                 for  (int i = 0; i < len;  i++)                {                    //if the letters are between 65-90, The position in the array is ASCII-65 and the number of times +1                   &nbSp;if (chararray[i]>=65 && chararray[i]<=90)                     {                                                  int hash=ch[i]-65;
if (count[hash]==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) {int hash=ch[i]-71;
                         if (Count[hash) ==1)                          {                              index  = i;                               break;                             }                    }                                   }                            }           return index;        } 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.