Algorithm 19: the first character that appears only once

Source: Internet
Author: User
Tags control characters

[Topic] finds the first character that appears only once in a string. For example, input string abaccdeff and Output B.

[Thu 1] Actually, the first response to this question is a hash table! Of course, I did this after I learned the hash table. For the past, our idea was of course to traverse strings from start to end. For every character that is traversed, compare each character after it with it. If there is no character equal to it, it is what we are looking. We can see thatAlgorithmThe time complexity is O (n2 ).

[Thinking 2] Of course, hash the table! All students who have learned the hash table know that the query speed of the hash table is O (1 ). My first reflection was to create a hash table with 26 letters. Of course, this does not contain any punctuation marks or control characters, so for the sake of comprehensiveness, we create a hash table with a length of 256 (each char occupies 8 bits and can represent only 256 types ). With this table, the following is simple. First, traverse the string and use the ASCII value of the character as the key value of the hash table, the value in the array stores the number of occurrences of each character. Traverse the string for the second time and retrieve the key value with the first value being 1. Based on this idea, we can easily get the followingCode:

 1 # Include <iostream>
2 # Include < String >
3 # Include <cstring>
4 Using Namespace STD;
5
6 Char Findfirstnotrepeatedchar ( Char * Pstring)
7 {
8 If (Pstring = NULL)
9 Return 0 ;
10
11 // Define and initialize a hash table
12 Const Int Hashlength = 256 ;
13 Unsigned Int Hashlist [hashlength];
14 For ( Int I =0 ; I 15 {
16 Hashlist [I] = 0 ;
17 }
18
19 // For the first time, the hash table records the number of occurrences of each character.
20 Char * Pchar = pstring;
21 While (* Pchar! = ' \ 0 ' )
22 {
23 Hashlist [* pchar] ++;
24 Pchar ++;
25 }
26
27 // The Pointer Points to the first character of the string.
28 Pchar = pstring;
29
30 // The second traversal, returns the first number of characters 1
31 While (* Pchar! = ' \ 0 ' )
32 {
33 If (Hashlist [* pchar] =1 )
34 {
35 Return * Pchar;
36 }
37
38 Pchar ++;
39 }
40
41 // No character appears only once
42 Return 0 ;
43 }
44
45 Int Main ()
46 {
47 Cout < " Please enter your string: " <Endl;
48 Const Int Maxsize = 100 ;
49 Char STR [maxsize];
50 Cin> STR;
51
52 Cout < " The first not repeated char is: " <Endl;
53 Cout <findfirstnotrepeatedchar (STR) <Endl;
54
55 Return 0 ;
56 }

The running result is as follows:

 

References:

ProgramMember interview questions featured 100 questions: http://zhedahht.blog.163.com/blog/static/25411174200722191722430/

Note:

1) Compile all the code environments in this blogWin7 + vc6. All code has been debugged by the blogger.

2 ) blog python27 Article is copyrighted, for Network reprint, please indicate the source http://www.cnblogs.com/python27/ . You have any suggestions for solving the problem. Please feel free to comment on them.

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.