Huawei machine exercise questions-repeated character filtering

Source: Internet
Author: User

Question:

  1. Compile a character string filter program. If multiple identical characters appear in the string, filter out non-first-time characters.
  2. For example, the "abacacde" character string is "ABCDE ".
  1. Example
  2. Input: "deefd" output: "def"
  3. Input: "afafaf" output: "AF"
  4. Input: "pppppppp" output: "P"
Analysis: we can see that the same character filtering is similar. If Java is used, we should first think of the set, which can effectively process repeated elements, the next step is the sequence problem. Here we keep the original sequence of characters, so we have to use a set class to store these characters. That's right, hashset can solve our problem very well, the next step is coding.


The Code is as follows:


Package com. wenj. test;

Import java. util. iterator;
Import java. util. linkedhashset;
Import java. util. Set;

/**
* Question:

Compile a character string filter program. If multiple identical characters appear in the string, filter out non-first-time characters.
For example, the "abacacde" character string is "ABCDE ".

Example
Input: "deefd" output: "def"
Input: "afafaf" 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 to filter characters
CS. Add (str2c [I]);
}

String temp = "";
For (iterator <character> it = cs. iterator (); it. hasnext ();) {// reassembles the string
Temp + = it. Next ();
}

Return temp;
}
}


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.