Lesson 2
String Manipulation and Frequency Analysis with Alice in Java
Introduction

Hello! Are you ready for an exciting voyage into the wonderful realm of strings and data structures? Today, we will assist Alice, an aspiring cryptographer, with an intriguing string manipulation task. She loves playing with strings and has come up with a unique string encoding scheme. I assure you, this will be an enlightening journey that will stretch your programming muscles. Let's get started!

Task Statement

Alice has devised a unique way of encoding words. She takes a word and replaces each character with the next character in the alphabetical order. In other words, given a string word, for each character, if it is not z, she replaces it with the character that comes next alphabetically. For the character z, she replaces it with a.

Another element of Alice's algorithm involves frequency analysis. After shifting the characters, she counts the frequency of each character in the new string. Then, she creates an association of each character with its frequency and ASCII value. Each character maps to a number, which is a product of the ASCII value of the character and its frequency. Our task is to construct a list containing these products, sorted in descending order.

Example

For the input string "banana", the output should be [294, 222, 99].

The string "banana" will be shifted to "cbobob".

Calculating the product of frequency and ASCII value for each character:

  • The ASCII value for c is 9999; it appears once in the string, so its product is 991=9999 \cdot 1 = 99.
  • The ASCII value for b is 9898; it appears three times in the string, so its product is 983=29498 \cdot 3 = 294.
  • The ASCII value for o is 111111; it appears twice in the string, so its product is 1112=222111 \cdot 2 = 222.

Collecting these products into a list gives [99, 294, 222]. Sorting this list in descending order results in [294, 222, 99].

Solution Building: Step 1 - Mapping each character to the next alphabetical character

Our first step involves mapping each character of the input string to the next alphabetical character. For this, we define the nextString as an empty StringBuilder, storing the shift operation result. We then iterate over each character of the input string. If a character is not z, we replace it with the next alphabetical character. If it is z, we replace it with a.

Here's the updated function in Java:

Java
1public static String characterFrequencyEncoding(String word) { 2 StringBuilder nextString = new StringBuilder(); 3 for (char letter : word.toCharArray()) { 4 nextString.append(letter == 'z' ? 'a' : (char) (letter + 1)); 5 } 6 return nextString.toString(); 7}
Solution Building: Step 2 - Counting the frequency of characters in `nextString`

The next step is to track the frequency of each character in nextString. We start by initializing an empty HashMap<Character, Integer>, frequencyMap. Then, we iterate over nextString. If the current character exists in frequencyMap, we increment its frequency by 1. If it doesn't exist, we add it to frequencyMap with a frequency of 1.

Incorporating this step into the function, our code now looks like this:

Java
1public static Map<Character, Integer> countFrequency(String nextString) { 2 Map<Character, Integer> frequencyMap = new HashMap<>(); 3 for (char letter : nextString.toCharArray()) { 4 frequencyMap.put(letter, frequencyMap.getOrDefault(letter, 0) + 1); 5 } 6 return frequencyMap; 7}
Solution Building: Step 3 - Building the product list

Next, we calculate the numerical representation for each unique character. We initialize an empty ArrayList<Integer>, combinedValues, to store these numbers. For each character in frequencyMap, we calculate the product of its ASCII representation and its frequency in nextString and append this to combinedValues.

Here's the updated function:

Java
1public static List<Integer> buildProductList(Map<Character, Integer> frequencyMap) { 2 List<Integer> combinedValues = new ArrayList<>(); 3 for (Map.Entry<Character, Integer> entry : frequencyMap.entrySet()) { 4 combinedValues.add((int) entry.getKey() * entry.getValue()); 5 } 6 return combinedValues; 7}
Solution Building: Step 4 - Sorting the final values

The final step is to sort the list combinedValues in descending order using Collections.sort.

Here's our complete function:

Java
1import java.util.ArrayList; 2import java.util.Collections; 3import java.util.HashMap; 4import java.util.List; 5import java.util.Map; 6 7public class CharacterFrequencyEncoding { 8 public static List<Integer> characterFrequencyEncoding(String word) { 9 // Step 1: Mapping each character to the next alphabetical character 10 StringBuilder nextString = new StringBuilder(); 11 for (char letter : word.toCharArray()) { 12 nextString.append(letter == 'z' ? 'a' : (char) (letter + 1)); 13 } 14 15 // Step 2: Counting the frequency of characters in nextString 16 Map<Character, Integer> frequencyMap = new HashMap<>(); 17 for (char letter : nextString.toString().toCharArray()) { 18 frequencyMap.put(letter, frequencyMap.getOrDefault(letter, 0) + 1); 19 } 20 21 // Step 3: Building the product list 22 List<Integer> combinedValues = new ArrayList<>(); 23 for (Map.Entry<Character, Integer> entry : frequencyMap.entrySet()) { 24 combinedValues.add((int) entry.getKey() * entry.getValue()); 25 } 26 27 // Step 4: Sorting the final values in descending order 28 combinedValues.sort(Collections.reverseOrder()); 29 30 // Return the sorted list 31 return combinedValues; 32 } 33 34 public static void main(String[] args) { 35 String word = "banana"; 36 List<Integer> result = characterFrequencyEncoding(word); 37 for (int value : result) { 38 System.out.print(value + " "); 39 } 40 // Prints: 41 // 294 222 99 42 } 43}
Lesson Summary

Well done! You've successfully tackled an intricate problem that required you to exercise multiple topics such as string manipulation, map processing, and list sorting. This task underscored the importance of reusing already calculated values. I encourage you to apply what you've learned today to other tasks. Many more exciting challenges are waiting for you in the upcoming practice sessions. Happy coding!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.