[algorithm] removes repeated characters from a string

Source: Internet
Author: User

How to delete repeated characters in a string

Problem Description:

Remove repeated characters from a string, for example, "good" removes the duplicated string and becomes "God".

The first method:

"Brute Force method", the simplest way is to think of this string as a character array, the array using a double loop to traverse, if you find a duplicate character, the character is set to ' ", and then the character array of all the ' \ 0 ' is removed, and the resulting string is the target string after the duplicate character is deleted.

The code is as follows:

Package removes repeated characters from the string; Public classSolution { Public StaticStringremoveduplicate(String str) {Char[] C = Str.tochararray ();intlen = c.length; for(inti =0; i < Len; i++) {if(C[i] = =' + ') {Continue; } for(intj = i +1; J < Len; J + +) {if(C[j] = =' + ') {Continue; }//Set the repeating character to ' + '                if(C[i] = = C[j]) {C[j] =' + '; }            }        }intL =0;//Remove ' /'         for(inti =0; i < Len; i++) {if(C[i]! =' + ') {C[l] = C[i];            l++; }        }return NewString (c,0, l); } Public Static void Main(string[] args) {String str ="ABCAABCD";        str = removeduplicate (str); System. out. println (str); }}

This algorithm uses a double loop with a time complexity of O (n^2)

The second method:

Space for time, due to the common characters only 256, you can assume that the string of different characters of a maximum of 256, then you can request an array of type int of size 256 to record the number of occurrences of each character, the initialization is 0, the character encoding as the subscript of the array, when traversing the character array , if the number of occurrences of this character is 0, then it is set to 1, if the number of occurrences of this character is 1, indicating that the character has appeared in the front, you can put this character as ' "," to achieve the purpose of the weight. This method requires only one traversal and the time complexity is O (n), but an additional 256 size space is required.

Package removes repeated characters from the string; Public  class Solution2 {     Public StaticString Removedaplicate (StringStr){Char[] C =Str. ToCharArray ();intlen = c.length;int[] flags =New int[8];//only 8 x 32bit int is required        intI for(i =0; I <8; i++) {Flags[i] =0; } for(i =0; i < Len; i++) {int Index= (int) c[i]/ +;intShift = (int) c[i]% +;if((flags[Index] &1<< shift)! =0) {C[i] =' + '; } flags[Index] |= (1<<shift); }intL =0; for(i =0; i < Len; i++) {if(C[i]! =' + ') {c[l++] = C[i]; }        }return NewString (c,0, l); } Public Static voidMain (string[] args) {StringStr="ABCAABCD";Str= Removedaplicate (Str); System.out.println (Str); }}

[algorithm] removes repeated characters from 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.