Prompt:
public class Solution {

  /**
   * Calculate the energy of the string, which is defined as the length of the longest non-empty substring containing only one character.
   *
   * @param s The input string.
   * @return The energy of the string.
   *
   * Example:
   * calculateEnergy("aabbccdd")
   * Output: 2
   */
  public static int calculateEnergy(String s)

Instruction:
Write a Java function `public static int calculateEnergy(String s)` to solve the following problem:
Calculate the energy of the string, which is defined as the length of the longest non-empty substring containing only one character.

    @param s The input string.
    @return The energy of the string.

    Example:
    calculateEnergy("aabbccdd")
    Output: 2

  {
    if (s == null || s.length() == 0) {
      return 0;
    }

    int maxEnergy = 1, currentEnergy = 1;
    char lastChar = s.charAt(0);

    // Start from the second character
    for (int i = 1; i < s.length(); i++) {
      char currentChar = s.charAt(i);
      if (currentChar == lastChar) {
        // Increment the energy of the current substring
        currentEnergy++;
      } else {
        // Update the maximum energy if needed and reset current energy
        maxEnergy = Math.max(maxEnergy, currentEnergy);
        currentEnergy = 1;
        lastChar = currentChar;
      }
    }

    // Ensure we check the last sequence energy
    maxEnergy = Math.max(maxEnergy, currentEnergy);

    return maxEnergy;
  }

  public static void main(String[] args) {
    // Test cases
    assert calculateEnergy("aabbccdd") == 2 : "Test case 1 failed"; // Contains two characters 'aa', 'bb', 'cc', 'dd'
    assert calculateEnergy("horizon") == 1 : "Test case 2 failed"; // All characters are unique
    assert calculateEnergy("success") == 2 : "Test case 3 failed"; // Contains three characters 'ccc'

    System.out.println("All tests passed");
  }
}


Prompt:
import java.util.ArrayList;
import java.util.List;

public class Solution {

  /**
   * Return all integers in the range [1, n] in lexicographical order.
   *
   * @param n The upper limit of the range.
   * @return List of integers in lexicographical order.
   *
   * Example:
   * lexicalOrder(13)
   * Output: [1,10,11,12,13,2,3,4,5,6,7,8,9]
   */
  public static List<Integer> lexicalOrder(int n)

Instruction:
Write a Java function `public static List<Integer> lexicalOrder(int n)` to solve the following problem:
Return all integers in the range [1, n] in lexicographical order.

    @param n The upper limit of the range.
    @return List of integers in lexicographical order.

    Example:
    lexicalOrder(13)
    Output: [1,10,11,12,13,2,3,4,5,6,7,8,9]

  {
    List<Integer> result = new ArrayList<>();
    for (int i = 1; i < 10; i++) { // start with all numbers from 1 to 9
      dfs(i, n, result);
    }
    return result;
  }

  private static void dfs(int current, int n, List<Integer> result) {
    if (current > n) {
      return; // stop the recursion if current is greater than n
    }
    result.add(current); // add current number to the result list
    for (int i = 0; i < 10; i++) { // traverse the next digit
      if (10 * current + i > n) {
        return; // stop if the next number is greater than n
      }
      dfs(10 * current + i, n, result); // recurse with next number
    }
  }

  public static void main(String[] args) {
    // Test cases
    assert lexicalOrder(13)
      .equals(
        new ArrayList<Integer>() {
          {
            add(1);
            add(10);
            add(11);
            add(12);
            add(13);
            add(2);
            add(3);
            add(4);
            add(5);
            add(6);
            add(7);
            add(8);
            add(9);
          }
        }
      ) : "Test case 1 failed";

    assert lexicalOrder(20)
      .equals(
        new ArrayList<Integer>() {
          {
            add(1);
            add(10);
            add(11);
            add(12);
            add(13);
            add(14);
            add(15);
            add(16);
            add(17);
            add(18);
            add(19);
            add(2);
            add(20);
            add(3);
            add(4);
            add(5);
            add(6);
            add(7);
            add(8);
            add(9);
          }
        }
      ) : "Test case 2 failed";

    assert lexicalOrder(2)
      .equals(
        new ArrayList<Integer>() {
          {
            add(1);
            add(2);
          }
        }
      ) : "Test case 3 failed";

    System.out.println("All tests passed");
  }
}


Prompt:
import java.util.HashMap;

public class Solution {

  /**
   * Calculate the sum of unique elements in the given integer array.
   *
   * @param nums An array of integers.
   * @return The sum of unique elements.
   *
   * Example:
   * calculateUniqueSum(new int[]{1, 2, 3, 2})
   * Output: 4
   */
  public static int calculateUniqueSum(int[] nums)

Instruction:
Write a Java function `public static int calculateUniqueSum(int[] nums)` to solve the following problem:
Calculate the sum of unique elements in the given integer array.

   @param nums An array of integers.
   @return The sum of unique elements.

   Example:
   calculateUniqueSum(new int[]{1, 2, 3, 2})
   Output: 4

  {
    HashMap<Integer, Integer> hashMap = new HashMap<>();
    for (int num : nums) {
      hashMap.put(num, hashMap.getOrDefault(num, 0) + 1);
    }

    int sum = 0;
    for (int key : hashMap.keySet()) {
      if (hashMap.get(key) == 1) {
        sum += key;
      }
    }

    return sum;
  }

  public static void main(String[] args) {
    assert calculateUniqueSum(new int[] { 1, 4, 5, 1, 2, 4 }) == 7; // Test Case 1
    assert calculateUniqueSum(new int[] { 0, 0, 0, 0 }) == 0; // Test Case 2
    assert calculateUniqueSum(new int[] { -1, -2, -3, -2 }) == -4; // Test Case 3

    System.out.println("All tests passed");
  }
}


Prompt:
public class Solution {

  /**
   * Given an integer n, return the minimum number of perfect square numbers that sum up to n.
   *
   * @param n The target integer.
   * @return The minimum number of perfect square numbers.
   *
   * Example:
   * minPerfectSquareSum(12)
   * Output: 3
   *
   * Example:
   * minPerfectSquareSum(13)
   * Output: 2
   */
  public static int minPerfectSquareSum(int n)

Instruction:
Write a Java function `public static int minPerfectSquareSum(int n)` to solve the following problem:
Given an integer n, return the minimum number of perfect square numbers that sum up to n.

    @param n The target integer.
    @return The minimum number of perfect square numbers.

    Example:
    minPerfectSquareSum(12)
    Output: 3

    Example:
    minPerfectSquareSum(13)
    Output: 2

  {
    int[] dp = new int[n + 1];
    for (int i = 1; i <= n; i++) {
      dp[i] = Integer.MAX_VALUE;
      for (int j = 1; j * j <= i; j++) {
        dp[i] = Math.min(dp[i], dp[i - j * j] + 1);
      }
    }
    return dp[n];
  }

  public static void main(String[] args) {
    assert minPerfectSquareSum(26) == 2 : "Test case 1 failed";
    assert minPerfectSquareSum(17) == 2 : "Test case 2 failed";
    assert minPerfectSquareSum(99) == 3 : "Test case 3 failed";
    System.out.println("All tests passed");
  }
}


Prompt:
public class Solution {

  /**
   * Reverse the characters of each word in the given string while maintaining the original word order.
   *
   * @param s The input string containing words separated by spaces.
   * @return The string with characters of each word reversed.
   *
   * Example:
   * reverseWords("Mr Ding")
   * Output: "rM gniD"
   */
  public static String reverseWords(String s)

Instruction:
Write a Java function `public static String reverseWords(String s)` to solve the following problem:
Reverse the characters of each word in the given string while maintaining the original word order.

    @param s The input string containing words separated by spaces.
    @return The string with characters of each word reversed.

    Example:
    reverseWords("Mr Ding")
    Output: "rM gniD"

  {
    String[] words = s.split(" ");
    StringBuilder reversedString = new StringBuilder();
    for (String word : words) {
      StringBuilder reverse = new StringBuilder(word).reverse();
      reversedString.append(reverse.toString()).append(" ");
    }
    return reversedString.toString().trim();
  }

  private static void testReverseWords(String input, String expectedOutput) {
    assert reverseWords(input).equals(expectedOutput) : "Test failed";
  }

  public static void main(String[] args) {
    testReverseWords("The quick brown fox", "ehT kciuq nworb xof");
    testReverseWords("Hello World", "olleH dlroW");
    testReverseWords("a b c d e f", "a b c d e f");
    System.out.println("All tests passed");
  }
}