Expand The Compressed String (a4b3g5) to (aaaabbbggggg) in Data Structure Using Java


What if  we have compressed String, and we have to expand it. Like my previous post that was about compression like is below:
Compress String (aaabbbcccc to a3b3c4)
So now we will convert compressed String to Expanded String
Now we check if character in the String is character of not?
If its character then add in new String data
else
(obviously there would be the number)
        we will convert this character into integer and we will run loop till that character and we will               add more characters like before of the number. And at the end of  the character we will have expanded String: Wow



Algorithm to Expand the String


class ExpandString 
         +expandString(data: String) : String
                   var temp:String// initial value=""
                   for(till length of data)
                          if((data.charAt(i) is character >='a' and character<='z' )) || ((data.charAt(i) is character >='A' and character<='Z' )))
                                  temp+=data.charAt(i)
                         // end if
                          else
                              int number = Integer.parseInt(data.charAt(i)+"");
                              for(till number)
                                     temp+=data.charAt(i-1);//because at i position is that number previous is its                                                                                   //character
                             // end for
                         // end else
                    // end for     
           return temp    // now temp is expanded String 
           end expandString()
end class




Comments

Popular Posts