[Offoffoffer] the first character that appears only once

Source: Internet
Author: User

 

 

Description:

Find the first occurrence character in a string (1 <= String Length <= 10000, all consisting of uppercase letters.

Input:

Multiple groups of data are input.
Enter a string for each group.

Output:

Output the first character subscript that appears only once. If the character does not appear only once, the output is-1.

Sample input:
ABACCDEFFAA
Sample output:
1-1

To solve the problem of repeated or frequent occurrence in strings, the most common one is the hash table. The character in the string is used as the key, and the number of occurrences of the character is used as the value. It is assumed that there are only characters within the ASCII code range, you can open up a 256-size int array, map each character (key) to the corresponding position of the array, calculate the number of occurrences, and traverse the string once, calculates the number of occurrences of each character, stores it in the corresponding position of the int array, and traverses the string for the second time, if the first occurrence of a character pair corresponds to the corresponding position of the hash table where the element is 1, then this character is the first character that appears only once, if we traverse the hash table (int array), the character corresponding to the position where the first element in the hash table is 1 is the first smallest character in the string that appears only once. The time complexity is O (n), which requires an additional 256 int space. It can be seen that the space complexity is O (1 ).

In addition, to save space, we can use the bitmap Algorithm to represent the number of occurrences of the corresponding character in two bits. If the occurrence is 0, it is 00, and if the occurrence is 01, if there are two or more times, you can maintain them at 10.

In addition, note that the char range is-128-127, and the unsigned char range is 0-255. Therefore, the ASCII value is between 128-255. If it is saved as a char type, the conversion range to int value is between-128--1, which is embodied in the following code.

 

The following code uses a simple hash table AC (two functions are written according to the question requirements and test requirements ):

 

# Include
           
            
# Include
            
             
/* Returns the first occurrence character */char FirstOnceChar (char * str) {if (str = NULL) return '\ 0'; int hashtable [256]; memset (hashtable, 0, sizeof (hashtable); char * pCur = str; while (* pCur! = '\ 0') {if (* pCur> = 0) hashtable [* (pCur ++)] ++; elsehashtable [* (pCur ++) + 256] ++;} while (* str! = '\ 0') {int index; if (* str> = 0) index = * str; elseindex = * str + 256; if (hashtable [index] = 1) return * str; elsestr ++;} return '\ 0';}/* returns the subscript of the first character that appears */int IndexOfFirstOnceChar (char * str) {if (str = NULL) return-1; int len = strlen (str); int hashtable [256]; memset (hashtable, 0, sizeof (hashtable )); int I; for (I = 0; I
             
              
= 0) hashtable [str [I] ++; elsehashtable [str [I] + 256] ++ ;}for (I = 0; I
              
               
= 0) index = str [I]; elseindex = str [I] + 256; if (hashtable [index] = 1) return I;} return-1 ;} int main () {char str [10010]; while (gets (str )! = NULL) printf ("% d \ n", IndexOfFirstOnceChar (str); return 0 ;}
              
             
            
           
/*************************************** ***********************

 

 

Problem: 1283 User: mmc_maodun Language: C Result: Accepted Time:10 ms Memory:912 kb ****************************************************************/

 

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.