[100 questions] 17th questions (Google test)

Source: Internet
Author: User
I. Question: Find the "first" and "only once" characters in a string.

If abaccdeff is input, B is output.

Ii. Analysis of ideas:

Letter A: appears twice

Letter B: compliant with requirements

Letter C: appears twice

Letter D: appears once, but not the first

Letter e: appears once, but not the first

Letter f: appears twice

Therefore, the letter B meets the requirements.

3. How can we find qualified letters?

First, scan the character array to record the number of occurrences of each character.

Scan the character array again and find the first character with a count of 1.

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.

4. Source Code:

# Include <iostream> <br/> # include <string> <br/> using namespace STD; <br/> void findchar (string & S) <br/>{</P> <p> int STR [256]; <br/> for (INT I = 0; I <256; I ++) // initialize the hash table <br/>{< br/> STR [I] = 0; <br/>}< br/> for (INT I = 0; I <S. size (); I ++) // use the character's ASCII value as the array subscript <br/>{< br/> STR [s [I] ++; <br/>}< br/> for (INT I = 0; I <256; I ++) // The second access character array, the first value is 1 <br/>{< br/> If (STR [I] = 1) <br/>{< br/> printf ("% C", I); <br/> return; <br/>}< br/> int main () <br/>{< br/> string S; <br/> cout <"enter a string" <Endl; <br/> CIN> S; <br/> findchar (s ); <br/> cout <Endl; <br/> return 0; <br/>}

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.