Find the first character in the string that appears only once

Source: Internet
Author: User

Find the first character in the string that appears only once

Find the first unique character in a string and you can just traverse this string only one time. if there is no such character, just return '#' and '#' will not appear in the string, else return the character you find.

For example: "aAbBABac", return 'B', and to "aBBa" return '#' as the final result.

Method 1:

As you are familiar with, we used to allow re-traversal of the string when finding the first character that only appears once. The solution is to create a hash table, hash [256], repeat the string, record the number of occurrences of each character, and then repeat the string to view the number of occurrences of each character. If the number of occurrences is 1, this character is printed, the traversal is stopped. However, when you only need to traverse strings once, you can only get the number of occurrences of each character, but cannot get the order of the original characters in the string. If you can think of this, you can solve the problem. Re-set an order array to record the order in which the characters appear, traverse the order array, retrieve the character that appears for the first time, and find the number of times it appears in the hash table, if the number of times is 1, the matching string is found. The Code is as follows:

 

Char first_unique_word (char * s) {if (strlen (s) = 0) exit (0); if (strlen (s) = 1) return s [0]; const unsigned int length = strlen (s); vector
 
  
Num (256, 0); vector
  
   
Order (256, 0); char res = '#'; for (int I = 0; I
   
    
Method 2
    

 

Method 1 uses an order array to record the elements that appear in sequence. After traversing the original string, you also need to conveniently query the order array at a time complexity of O (2n ), in fact, the character sequence has been recorded in the string. We can find a way to directly read the characters without traversing the order array. This character must be unique because it is the first occurrence of a character in a string. Therefore, we traverse this string from the back to the front, and the last element that is added to the order never appears and appears for the first time. The Code is as follows:

 

char first_unique_word1(char *s){if (strlen(s)==0)  exit(0);if (strlen(s)==1)  return s[0];const unsigned int length=strlen(s);vector
     
       num(256,0);vector
      
       order(256,0);char res='#';int j=0;for (int i=length-1;i>=0;i--){if (s[i]!='/0'){num[s[i]]++;if (num[s[i]]==1){order[j]=s[i];j++;}}}if (num[order[j-1]]==1){res=order[j-1];}return res;}
      
     

The complete code is as follows:

 

 

#include
     
      #include 
      
       using namespace std;char first_unique_word(char *s);char first_unique_word1(char *s);void main(){char s[]="abbfabbcde";cout<
       
         num(256,0);vector
        
         order(256,0);char res='#';for (int i=0;i
         
           num(256,0);vector
          
           order(256,0);char res='#';int j=0;for (int i=length-1;i>=0;i--){if (s[i]!='/0'){num[s[i]]++;if (num[s[i]]==1){order[j]=s[i];j++;}}}if (num[order[j-1]]==1){res=order[j-1];}return res;}
          
         
        
       
      
     

The above code is output

 

F

F


 

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.