Part (a)
/** @param parts an ArrayList of string parts that are valid in the master string
* Precondition: parts.size() > 0
* @return the string obtained by concatenating the parts of the master string
*/
public String decodeString(ArrayList<StringPart> parts)
{
String word = "";
for (StringPart p : parts)
{
int start = p.getStart();
word += masterString.substring(start, start + p.getLength());
}
return word;
}
Part (b)
/** @param word the string to be encoded
* Precondition: all of the characters in word appear in the master string;
* word.length() > 0
* @return an ArrayList of string parts of the master string that can be combined
* to create word
*/
public ArrayList<StringPart> encodeString(String word)
{
ArrayList<StringPart> parts = new ArrayList<StringPart>();
while (word.length() > 0)
{
StringPart p = findPart(word);
parts.add(p);
word = word.substring(p.getLength()); 1
}
return parts;
}
Notes:
- Chop off the processed substring from the beginning of
word .
|