Introduction to Algorithms: Character statistical problems

Source: Internet
Author: User

 In the ACM home to see an interesting algorithm problem, many similar to Huawei, Google and other IT giant crocodile, as a template to examine interns.

(1) The question form of Huawei is: Enter a string of lowercase letters (A~Z) through the keyboard, write a string filter program, if there are multiple identical characters in the string, filter out characters that are not the first occurrence. For example, the input string "ABACACDE", the output filter result is "ABCDE".

It is not a difficult problem, with the enumeration method, a double cycle can be done. The sample code is as follows:

1  PackageOrg.warnier.zhang.demo;2 3  Public classStringfilter {4 5      Publicstring Filter (String origin) {6 String result;7         if(Origin = =NULL) {8result =NULL;9}Else {Ten             Char[] chars =Origin.tochararray (); OneStringBuilder SB =NewStringBuilder (); A  -              for(inti = 0; i < chars.length; i++) { -                 intj = 0; the                  for(; J < I; J + +) { -                     if(Chars[i] = =Chars[j]) { -                          Break; -                     } +                 } -                 if(J = =i) { + sb.append (Chars[i]); A                 } at             } -result =sb.tostring (); -         } -         returnresult; -     } -}

The solution given by the official website is more ingenious, the following translation for the Java version:

1      Publicstring Filter (String origin) {2 String result;3         if(Origin = =NULL){4result =NULL;5}Else{6             /**7 * To determine whether a character appears in the logo;8              */9             Boolean[] flags =New Boolean[26];Ten              for(inti = 0; i < flags.length; i++){ OneFlags[i] =false; A             } -             Char[] chars =Origin.tochararray (); -StringBuilder SB =NewStringBuilder (); the             /** - * Determine if the character appears for the first time; -              */ -              for(inti = 0; i < chars.length; i + +){ +                 if(false= = Flags[chars[i]-' a ']){ -Flags[chars[i]-' a '] =true; + sb.append (Chars[i]); A                 } at             } -result =sb.tostring (); -         } -         returnresult; -}

  (2) Google's question form is: Find the first occurrence of a character in a string, such as input Abaccdeff, then output B.

  If you are in advance to see the Huawei interview interns are more fortunate, in the absence of the complexity of the algorithm requirements, the above code simple modification is done. Such as  

1      Publicstring Filter (String origin) {2String result =NULL;3         if(Origin! =NULL) {4             Char[] chars =Origin.tochararray ();5              for(inti = 0; i < chars.length; i++) {6                  for(intj = 0; J < I; J + +) {7                     if(Chars[i] = =Chars[j]) {8Chars[i] = ' 0 ';9CHARS[J] = ' 0 ';Ten                          Break; One                     } A                 } -             } -              for(inti = 0; i < chars.length; i++) { the                 if(chars[i]! = ' 0 ') { -result =string.valueof (Chars[i]); -                      Break; -                 } +             } -         } +         returnresult; A}

The official website also gives a new solution, translated into the Java version as follows:

1  Publicstring Filter (String origin) {2String result =NULL;3         if(Origin! =NULL){4             /**5 * Save the number of characters appearing;6              */7             int[] flags =New int[26];8              for(inti = 0; i < flags.length; i++){9Flags[i] = 0;Ten             } One             /** A * Count the number of characters appearing; -              */ -             Char[] chars =Origin.tochararray (); the              for(inti = 0; i < chars.length; i++){ -Flags[chars[i]-' a ']++; -             } -              for(inti = 0; i < flags.length; i++){ +                 if(Flags[i] = = 1){ -result = String.valueof ((Char) (' a ' +i)); +                      Break; A                 } at             } -         } -         returnresult; -}

In fact, the answer to the official website is not difficult to see with the above answers to Huawei questions similar places. This from another point of view, this exam although Huawei, Google's inspection angle is different, but original aim, the process of solving the problem to clear the fog, see the essence.

Furthermore, the official website of the problem-solving strategy is also a clever way of thinking, I have in one of my blog "Share: Genericcalendar Tool Class", the use of similar to the "judging whether the character appears", "Save the number of occurrences" flag amount of ways to deal with the flat, leap year February days of choice.

Introduction to Algorithms: Character statistical problems

Related Article

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.