"Sword Point offer": [55] The first non-repeating character in a character stream

Source: Internet
Author: User

Title: Implement a function to find the first character in a character stream that appears only once. For example, when you read only the first two characters "go" from a character stream, the first character that appears only once is "G". When the first six characters "Google" are read from the stream, the character that appears only once is "L".
This is similar to the character in [35] where the first occurrence of the string is found. So the detailed process is not mentioned here.
Scenario One: Sequential scan. time Complexity O (n*n) + spatial Complexity O (N). After sequential scanning, record the number of occurrences of each character. Then the sequential scan array gets the first number of characters corresponding to 1.
Scenario Two: Hash table method. time complexity O (n) + spatial complexity 0 (N)-hash table. The method mentioned in [35] above is: Use the hash table to record the number of each character, using ASCII as the key value of the hash table, and the number of occurrences of the character as the hash table value. This problem is also used in a hash table to record, ASCII as the key value of the hash table, the position of the character corresponding to, also can be understood as subscript as the value of the hash table. For example, the array is initialized to-1, the first character that appears is 0, the second one appears is 1 ... Once again, if it appears again, set to-2, and at the end of the search, look for the first number equal to 0.
The following code is primarily used to simulate character stream manipulation using strings.
The specific implementation code is:
#include <iostream>using namespace Std;int index=0;int occurrence[256];void ReSet () {for (int i=0;i<256;i++) Occurrence[i]=-1;index=1;} void InSert (char *str) {while (*str!= ') {if (occurrence[*str]==-1) occurrence[*str]=index;//first appears; else if ( occurrence[*str]>=0) occurrence[*str]=2;//appear here; str++;}} Char firstapperance (char *str) {char ch= ' + '; while (*str!= '} ') {if (occurrence[*str]==1) {Ch=*str;return ch;} str++;} return ch;} int main () {char *strr[3]={"abcdabc", "ABCABC", "abc"};for (int i=0;i<3;i++) {ReSet (); InSert (Strr[i]);//read in character; char Result=firstapperance (Strr[i]); if (Result! = ') cout<<strr[i]<< ": The first occurrence of the character is:" <<result< <endl;elsecout<<strr[i]<< ": The character in the string does not appear once! "<<endl;} System ("pause"); return 0;}

Operation Result:


"Sword Point offer": [55] The first non-repeating character in a character stream

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.