java代写|CS代写|算法代写-Compress practice

java代写|CS代写|算法代写: 这是一个小的java编程训练

Write your code in the file Compress.java. Your code should go into a method with the following signature. You may write your own main method to test your code. Auto lab will ignore your main method:

public static String compress ( String original ){}

Run-length encoding (RLE) is a simple "compression algorithm" (an algorithm whichtakes a block of data and reduces its size, producing a block that contains the sameinformation in less space). It works by replacing repetitive sequences of identical dataitems with short "tokens" that represent entire sequences. Applying RLE to a stringinvolves finding sequences in the string where the same character repeats. Each suchsequence should be replaced by a "token" consisting of:

  1. the number of characters in the sequence
  2. the repeating character

If a character does not repeat, it should be left alone.

For example, consider the following string:

qwwwwwwwwweeeeerrtyyyyyqqqqwEErTTT

After applying the RLE algorithm, this string is converted into:

q9w5e2rt5y4qw2Er3T

In the compressed string, "9w" represents a sequence of 9 consecutive lowercase "w"characters. "5e" represents 5 consecutive lowercase "e" characters, etc.

Write a method called compress that takes a string as input, compresses it using RLE,and returns the compressed string. Case matters – uppercase and lowercase charactersshould be considered distinct. You may assume that there are no digit charactersin the input string. There are no other restrictions on the input – it may containspaces or punctuation. There is no need to treat non-letter characters any differentlyfrom letters.

发表评论

电子邮件地址不会被公开。 必填项已用*标注