[Leetcode] Remove Duplicate Letters

Source: Internet
Author: User

Topic:

"Bcabc""abc""CBACDCBC""acdb"

Analysis: By observing the rule of each letter subscript, taking "CBACDCBC" as an example,

The first step is to calculate the subscript (Countindex):

A  = 2,  b  = 1, 6c  = 0, 3, 5, 7D  and 4

The second step is to find the letter that meets the criteria (Findletter):

If the first subscript of a letter is less than the last subscript for each letter thereafter , the letter meets the criteria. For example, a of 2 is less than B of the 6,c of 7,d 4, then a 2 meet the conditions.

In the third step, remove the preceding subscript (Removeindex):

To set the letter x that matches the condition in the second step, delete all subscripts before the x subscript and delete x. For example, if X is a,x subscript 2, remove 0 from 1,c in B and remove a.

Repeat step two, step three until the collection is empty.

Take "CBACDCBC" as an example:

b  = 6c  = 3, 5, 7D  and 4
b  = 6d  = 4

The above transformation interpretation: Although 6 < 7, but 6 but > 4, so B does not meet, and C in 3 is less than the subsequent 4, so C in accordance with.

b  = 6

Finally, a-C,-B, acdb

The code is as follows:

     Publicstring Removeduplicateletters (string s) {StringBuilder res=NewStringBuilder (); HashMap<character, arraylist<integer>> HM =NewHashmap<character, arraylist<integer>>(); ArrayList<Character> reference =NewArraylist<character>();        Countindex (S, HM, reference);        Collections.sort (reference);  while(!Reference.isempty ()) {            intLettindex =Findletter (S, res, HM, reference);        Removeindex (Lettindex, HM, reference); }        returnres.tostring (); }        Private voidCountindex (String s, Hashmap<character, arraylist<integer>> hm, arraylist<character>reference) {         for(inti = 0; I < s.length (); i++) {            CharCH =S.charat (i); if(!hm.containskey (CH)) {ArrayList<Integer> tmp =NewArraylist<integer>();                Tmp.add (i);                Hm.put (CH, tmp);            Reference.add (CH); } Else{hm.get (ch). Add (i); }        }    }        Private intFindletter (String S, StringBuilder res, hashmap<character, arraylist<integer>> hm, Arraylist<character >reference) {        intm = 0;  for(inti = 0; I < reference.size (); i++) {m= Hm.get (Reference.get (i)). Get (0); intj = i+1;  for(; J < Reference.size (); j + +) {ArrayList<Integer> tmp =Hm.get (Reference.get (j)); if(M > Tmp.get (Tmp.size ()-1)) {                     Break; }            }            if(J = =reference.size ())                {Res.append (Reference.get (i));                Reference.remove (i);  Break; }        }        returnm; }        Private voidRemoveindex (intLett, Hashmap<character, arraylist<integer>> hm, arraylist<character>reference) {         for(inti = 0; I < reference.size (); i++) {ArrayList<Integer> tmp =Hm.get (Reference.get (i));  while(!tmp.isempty () && tmp.get (0) <Lett) {Tmp.remove (0); }        }    }

[Leetcode] Remove Duplicate Letters

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.