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"
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;
}
}