[Offoffoffoffer] the first character that appears only once

Source: Internet
Author: User

Reprinted please indicate the source: http://blog.csdn.net/ns_code/article/details/27106997

Description:

Find the first character that appears only once 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.

Example input:
ABACCDEFFAA
Example output:
1-1

The hash table is used to handle repeated or frequent occurrences of strings. The key is used as the character in the string, and the number of occurrences of the character is used as the value, assuming that there are only characters in the ASCII code range, an int array of 256 size can be opened, and each character (key) can be mapped to the corresponding position of the array, calculate the number of times each character appears. traverse the string once, calculate the number of times each character appears, save it to the corresponding position of the int array, and traverse the string for the second time, if the first occurrence of a character corresponding to the corresponding position in the hash table is 1, the character is the first character that appears only once, suppose we traverse the hash table (INT array), then the first element in the hash table is 1, and the corresponding character is the first smallest character in the string that appears only once. The time complexity is O (n), and an additional 256 int spaces are required to assist. 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 characters in two bits. If the occurrence is 0, it is 00, and if the occurrence is 01, if there are two or more times, it can be maintained at 10.

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

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

# Include <stdio. h> # include <string. h>/* return 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 <Len; I ++) {If (STR [I]> = 0) hashtable [STR [I] ++; elsehashtable [STR [I] + 256] ++;} for (I = 0; I <Len; I ++) {int index; if (STR [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 ****************************************************************/

[Offoffoffoffer] the first character that appears only once

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.