Finds the first occurrence of a character in a string

Source: Internet
Author: User

Title Description:

The first occurrence of a character is found in a string (1<= string length <=10000, all composed of uppercase letters). If you enter Abaccdeff, then output B.

Input:

Enter more than one set of data
Enter a string for each group.

Output:

Output the first occurrence of a character Poute, no one-time characters are output-1

We have two ways to solve this problem.

1. Brute force method.

Iterating through the array, each character is found to traverse the entire array, to see if the character exists in the array below the table is inconsistent, but the Assic code value of the same character exists, if it does not exist, it is returned that character, if present, continue to traverse. The time complexity of the algorithm is O (n*n), obviously this is not very high efficiency.

1 /**2 * Using two cycles, time complexity O (n*n)3      * 4      * @authorYfy5      * @paramargs6 * String to search for7      * @paramLen8 * String Length9      * @returnreturns the Assic code value of the first occurrence of a character, or 1 if there are no characters appearing only once.Ten      */ One     intFirstnotrepeatingchar (Char[] args,intLen) { A         intJ; -          for(inti = 0; i < Len; i++) { -              for(j = 0; J < Len; J + +) { the                 if(I! =j) { -                     if(Args[i] = =Args[j]) { -                          Break; -                     } +                 } -             } +             //No characters are found that are equal to this character.  A             if(J = =Len) { at                 returnArgs[i]; -             } -         } -         return-1; -}
View Code

Finds the first occurrence of a character in a string

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.