Removes consecutive and repeated characters from a string.
Objective: To remove consecutive repeated characters in a string.
Input: sequence: kkkkhan888shioobo66
The correct returned result should be: hanshibo
Analysis of ideas
1. Use a JAVA regular expression to match consecutive identical characters or numbers.
2. Find the matched sequence and put it in the list.
3. Sort the list. Repeat the sequence at the top. (This step can be omitted)
4. Find out the consecutive repeated subsequences and replace these consecutive repeated subsequences with null (character string.
5. Return the output.
Code
Public class Test {public static void main (String [] args) {String strings = matcher ("kkkhan888shioobo66"); System. out. println (strings);} public static String matcher (String input) {// create a List <String> List = new ArrayList <String> (); // create the matched Pattern pattern = Pattern. compile ("(.) \ 1 * "); // Matcher = pattern. matcher (input); // searches for the Child sequence that matches the pattern. Find the child sequence that matches this mode from "+ kkkhan888shioobo66" (.) \ 1. If yes, true is returned. If no, false. while (matcher. find () {// returns the matched sub-sequence and adds it to the list. List. add (matcher. group ();} System. out. println (list); // sort the List in the group. Sorts the specified list based on the Generation sequence of the specified comparator. Repeat the sequence at the top. Collections. sort (list, new Comparator <String> () {public int compare (String o1, String o2) {return o2.length ()-o1.length ();}}); // find the repeated characters and add them to the array. String [] strings = list. toArray (new String [0]); // finds continuous and repeated subsequences. Replace these consecutive repeated subsequences with null strings. For (int I = 0; I <= strings. length-1; I ++) {if (strings [I]. length ()> 1) {System. out. println (strings [I]); input = input. replace (strings [I], ""); System. out. println (input) ;}} System. out. println ("Final Result:" + input); // returns the sequence of characters that are removed from consecutive duplicates. Return input ;}}
Java Regular Expression for multiple consecutive characters with the same character
([0-9]) \ 1 {5} Or ([\ d]) \ 1 {5} consecutively the same 6 digits such as: 333333
([0-9a-zA-Z]) \ 1 {5} Six consecutive digits or letters with the same name, for example, 222222 cccccc ZZZZZZ
([\ D]) \ 1 {2} ([a-z]) \ 2 {2} three consecutive digits after the root of the same three consecutive lowercase letters such as: 222www
([\ D]) \ 1 {2} ([a-z]) \ 2 {2} | ([a-z]) \ 3 {2} ([\ d]) \ 4 {2} is the same as above, but can match numbers + letters or letters + numbers such as 222www or www222
You can expand it by yourself. Note that \ 1 \ 2 represents the position and increments from left to right.
I am the dividing line of tiantiao
Reference: http://blog.csdn.net/atomcrazy/article/details/9087187
2. Write a function to remove consecutive duplicate letters from the string, for example, "aabbbcddddkkkmmmmaakk
What is the data type required by the question?
Pretend to be lowercase letters
# Define maxn255
Char * removeDup (char * s ){
Char p [26];
Char r [MaxN];
Int I, j = 0;
For (I = 26; I --; p [I] = 0 );
For (I = 0; I <= strlen (s); I ++)
If (! P [s [I]-'a']) {
P [s [I]-'a'] = 1;
R [j ++] = s [I];
}
R [j ++] = '\ 0 ';
Return r;
}
How can I remove repeated characters from a string in java?
Hello, lz.
This is a simple algorithm question. You can write it yourself.
But it is not necessary to use the Java language. Java comes with generic-related classes, which are very useful.
The TreeSet set can automatically identify whether or not the added entries are repeated. duplicate entries are not added, which is convenient.
The following is the implementation code:
Import java. util. list; import java. util. authorization; public class DeleteRepeated {private String str; private TreeSet <String> noReapted; // TreeSet generic public DeleteRepeated () {rule in = new rule (System. in); System. out. println ("enter a string:"); str = in. nextLine (); noReapted = new TreeSet () ;}// clear duplicate data public void removeRepeated () {for (int I = 0; I <str. length (); I ++) {noReapted. add ("" + str. charAt (I); // str. charAt (I) returns the char type. Therefore, adding a "" Space first and converting it to the String type // The TreeSet generic type can ensure that the duplicate is not added, and the order} str = ""; for (String index: noReapted) {str + = index;} // output System. out. println (str);} public static void main (String [] args) {DeleteRepeated dr = new DeleteRepeated (); dr. removeRepeated ();}}
Run:
Hope to help you