Huawei-on Practice--repetitive character filtering

Source: Internet
Author: User

Title:

    1. Write a string filter program, if you use multiple identical characters in the string, the characters first appear in non-filtered,.

    2. For example, the string "ABACACDE" Filter Results to "ABCDE".
    1. Demo Sample
    2. input: "DEEFD" Output: "Def"
    3. input: "AFAFAFAF" Output: "AF"
    4. input: "PPPPPPPP" Output: "P"
Analysis: See similar filtering of the same characters like this. In Java, the first thing we think about is the set, which is a collection that effectively handles the repeated elements. The next step is the order problem, which is to keep the characters in their original order, so we have to choose a class that implements set to store the characters. Yes, Linkedhashset can solve our problems very well. And the next thing to do is write the program.


The code is as follows:


Package com.wenj.test;

Import Java.util.Iterator;
Import Java.util.LinkedHashSet;
Import Java.util.Set;

/**
Topic

Please write a string filter program. If more than one of the same characters appears in the string, the characters that are not first appear are filtered out.
For example, the string "ABACACDE" Filter Results to "ABCDE".

Demo sample
Input: "DEEFD" Output: "Def"
Input: "AFAFAFAF" Output: "AF"
Input: "PPPPPPPP" Output: "P"
* @author wenj91-pc
*
*/
public class Teststrfilter {

public static void Main (String args[]) {
String Strin = "PPPPPPPP";
Teststrfilter ts = new Teststrfilter ();
System.out.println (Ts.strfilter (Strin));
}

public string strfilter (string strin) {
String strtemp = Strin;
char[] str2c = Strtemp.tochararray ();

Set<character> cs = new linkedhashset<character> ();
for (int i=0; i<str2c.length; i++) {//Use set for character filtering
Cs.add (Str2c[i]);
}

String temp = "";
For (iterator<character> it=cs.iterator (); It.hasnext ();) {//once again combining strings
Temp + = It.next ();
}

return temp;
}
}


Copyright notice: This article Bo Master original article. Blog, not reproduced without consent.

Huawei-on Practice--repetitive character filtering

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.