Programmer interview question selection (13): the first character that appears only once

Source: Internet
Author: User
Question: Find the first character that appears only once in a string. If abaccdeff is input, B is output.

Analysis: This question was written by Google in 2006.

The most intuitive idea is to scan each character in the string from the beginning. When a character is accessed, this character is compared with each character after it. If no duplicate character is found after it, it is a character that appears only once. If the string contains n characters, each character may be compared with the following O (n) characters. Therefore, the time complexity of this approach is O (n2 ). We try to find a faster method.

Since the question is related to the number of occurrences of a character, can we count the number of occurrences of each character in this string? To achieve this goal, we need a data container to store the number of occurrences of each character. In this data container, you can find the number of occurrences of a character. That is to say, the container maps a character into a number. Hash Tables are used in common data containers.

A hash table is a complex data structure. Due to the complexity, hash tables are not implemented in STL, so we need to implement one by ourselves. However, due to the particularity of this question, we only need a very simple hash table to meet the requirements. Because the character (char) is a data type with a length of 8, there are a total of 256 possibilities. Therefore, we create an array with a length of 256. Each letter uses its ASCII code value as the index corresponding to the array, and the array stores the number of times each character corresponds. In this way, we create a hash table with the size of 256 and the ASCII code as the key value.

When we scan this array for the first time, find the corresponding item in the hash table and increase the number of occurrences. In this way, the number of occurrences of each character can be obtained directly from the hash table during the second scan.

The reference code is as follows:

//////////////////////////////////////// ///////////////////////////////
// Find the first char which appears only once in a string
// Input: pstring-the string
// Output: the first not repeating char if the string has, otherwise 0
//////////////////////////////////////// ///////////////////////////////
Char firstnotrepeatingchar (char * pstring)
{
// Invalid input
If (! Pstring)
Return 0;

// Get a hash table, and initialize it
Const int tablesize = 256;
Unsigned int hashtable [tablesize];
For (unsigned int I = 0; I <tablesize; ++ I)
Hashtable [I] = 0;

// Get the how many times each char appears in the string
Char * phashkey = pstring;
While (* (phashkey )! = '/0 ')
Hashtable [* (phashkey ++)] ++;

// Find the first char which appears only once in a string
Phashkey = pstring;
While (* phashkey! = '/0 ')
{
If (hashtable [* phashkey] = 1)
Return * phashkey;

Phashkey ++;
}

// If the string is empty
// Or every char in the string appears at least twice
Return 0;
}

 

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.