Encode & Decode

Source: Internet
Author: User

Recently by various encode and decode algorithm abuse very faint, summed up, I hope there is a general solution??

The Compress a string to remove duplicates when a character occurs +2 times continuously.
e.g.
Input: "Abbbbbbbbbbbbbbc 3aaaa12bbxxx2 Hello"
Output: "A14xbc 1x34xa12bb3xx2 Hello".

Analysis Corner Case
* 1. Number + Repeat CHARACTER:3AAAAA-1x35xa
* 2. "Number" + "X" + "character" already exist in original STRING:3XA-1X31XXA

1  Public classcompressstring {2      Public Static voidMain (string[] args) {3 //String str = "ABBBBBBBBBBBBBBC 3aaaa12bb3x223444 hello";4String str = "123456C2XC";5         if(str = =NULL|| Str.length () = = 0)6             return;7StringBuilder output =NewStringBuilder ();8Output.append ("");9         inti = Str.length ()-1, J = str.length ()-1;Ten         intCur_num = 0; One         BooleanPrex =false; A         CharCur =Str.charat (j); -          while(I >= 0) { -             if(Str.charat (i) = =cur) { thei--; -}Else { -Cur_num = J-i; -                 if(cur >= ' 0 ' && cur <= ' 9 ') { +                     if(!prex && cur_num <= 2) { -                         intc = 0; +                          while(C <cur_num) { AOutput.insert (0, cur); atC++; -                         } -Prex =false; -}Else { -Output.insert (0, cur); -Output.insert (0, ' x '); inOutput.insert (0, cur_num); -Prex =true; to                     } +}Else if(Cur_num <= 2 && cur!= ' x ') { -                     intc = 0; the                      while(C <cur_num) { *Output.insert (0, cur); $C++;Panax Notoginseng                     } -Prex =false; the}Else { +Output.insert (0, cur); AOutput.insert (0, ' x '); theOutput.insert (0, cur_num); +Prex =true; -                 } $Cur =Str.charat (i); $j =i; -i--; -             } the         } -Cur_num = J-i;Wuyi         if(cur >= ' 0 ' && cur <= ' 9 ') { the             if(!prex && cur_num <= 2) { -                 intc = 0; Wu                  while(C <cur_num) { -Output.insert (0, cur); AboutC++; $                 } -}Else { -Output.insert (0, cur); -Output.insert (0, ' x '); AOutput.insert (0, cur_num); +             } the}Else if(Cur_num <= 2) { -             intc = 0; $              while(C <cur_num) { theOutput.insert (0, cur); theC++; the             } the}Else { -Output.insert (0, cur); inOutput.insert (0, ' x '); theOutput.insert (0, cur_num); the         } About System.out.println (output.tostring ()); the     } the}
View Code

No.2 there is a decode method F, which could
F ("somestring" + "x" + "Anynumber")--"somestring" "somestring" ... "somestring" (n times of such a string)
e.g.
Aaax-ax3x

Analysis Corner Case
* 1. Repeat string + Number:aaa3-ax2a3
* 2. "String" + "X" + "number" exists in the original input STRING:AX32AAA-

This is a little bit confusing for the moment.

Encode & Decode

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.