Claude vs ChatGPT: A Coder's Perspective on LLM Performance II

Well, Claude does it again. Even against the powerful GPT 4o with Canvas, there is no match compared with the kids from Anthropic.

Problem was this one: Total Characters in String After Transformations I - LeetCode

3335. Total Characters in String After Transformations I
Medium

You are given a string s and an integer t, representing the number of transformations to perform. In one transformation, every character in s is replaced according to the following rules:

  • If the character is 'z', replace it with the string "ab".
  • Otherwise, replace it with the next character in the alphabet. For example, 'a' is replaced with 'b', 'b' is replaced with 'c', and so on.

Return the length of the resulting string after exactly t transformations.

Since the answer may be very large, return it modulo 109 + 7.

Example 1:

Input: s = "abcyy", t = 2

Output: 7

Explanation:

  • First Transformation (t = 1):
    • 'a' becomes 'b'
    • 'b' becomes 'c'
    • 'c' becomes 'd'
    • 'y' becomes 'z'
    • 'y' becomes 'z'
    • String after the first transformation: "bcdzz"
  • Second Transformation (t = 2):
    • 'b' becomes 'c'
    • 'c' becomes 'd'
    • 'd' becomes 'e'
    • 'z' becomes "ab"
    • 'z' becomes "ab"
    • String after the second transformation: "cdeabab"
  • Final Length of the string: The string is "cdeabab", which has 7 characters.

Example 2:

Input: s = "azbk", t = 1

Output: 5

Explanation:

  • First Transformation (t = 1):
    • 'a' becomes 'b'
    • 'z' becomes "ab"
    • 'b' becomes 'c'
    • 'k' becomes 'l'
    • String after the first transformation: "babcl"
  • Final Length of the string: The string is "babcl", which has 5 characters.

Constraints:

  • 1 <= s.length <= 105
  • s consists only of lowercase English letters.
  • 1 <= t <= 105

My implementation usually is the worst, but works:

public int LengthAfterTransformations(string s, int t)
{
 BigInteger[] countChars = new BigInteger[26];
 long MOD = 1000000000 + 7;
 foreach (char c in s)
 countChars[(int)(c - 'a')]++;
 for (int i = 0; i < t; i++)
 {
 BigInteger ab = countChars[countChars.Length - 1];
 for (int j = countChars.Length - 1; j > 0; j--)
 countChars[j] = countChars[j - 1];
 countChars[0] = ab;
 countChars[1] += ab;
 }
 BigInteger retVal = 0;
 for (int i = 0; i < countChars.Length; i++)
 retVal += countChars[i];
 return (int)(retVal % MOD);
}

ChatGPT improved the performance significantly, but Anthropic Claude just killed it, with the following solution:

public int LengthAfterTransformations(string s, int t)
{
 const int MOD = 1_000_000_007;
 var counts = new long[26];
 
 // Count initial frequencies
 foreach (char c in s)
 {
 counts[c - 'a']++;
 }
 
 // Perform transformations
 for (int i = 0; i < t; i++)
 {
 // Store last character count (z)
 long lastChar = counts[25];
 
 // Shift all characters right
 for (int j = 25; j > 0; j--)
 {
 counts[j] = counts[j - 1];
 }
 
 // Handle wrapping of 'z' to 'a' and incrementing 'b'
 counts[0] = lastChar;
 counts[1] = (counts[1] + lastChar) % MOD;
 }
 
 // Calculate final sum
 long sum = 0;
 for (int i = 0; i < 26; i++)
 {
 sum = (sum + counts[i]) % MOD;
 }
 
 return (int)sum;
}

You can see that the gist of the solution is the same as mine, but just that they squeeze every single bit of performance, from the usage of const all the way to getting rid of the expensive BigInteger, and this is what they accomplished - cheers, ACC.




Comments

Post a Comment

[フレーム]

Popular posts from this blog

Quasi FSM (Finite State Machine) problem + Vibe

Not really an FSM problem since the state isn't changing, it is just defined by the current input. Simply following the instructions should do it. Using VSCode IDE you can also engage the help of Cline or Copilot for a combo of coding and vibe coding, see below screenshot. Cheers, ACC. Process String with Special Operations I - LeetCode You are given a string  s  consisting of lowercase English letters and the special characters:  * ,  # , and  % . Build a new string  result  by processing  s  according to the following rules from left to right: If the letter is a  lowercase  English letter append it to  result . A  '*'   removes  the last character from  result , if it exists. A  '#'   duplicates  the current  result  and  appends  it to itself. A  '%'   reverses  the current  result . Return the final string  result  after processing all char...

Shortest Bridge – A BFS Story (with a Twist)

Here's another one from the Google 30 Days challenge on LeetCode — 934. Shortest Bridge . The goal? Given a 2D binary grid where two islands (groups of 1s) are separated by water (0s), flip the fewest number of 0s to 1s to connect them. Easy to describe. Sneaky to implement well. 🧭 My Approach My solution follows a two-phase Breadth-First Search (BFS) strategy: Find and mark one island : I start by scanning the grid until I find the first 1 , then use BFS to mark all connected land cells as 2 . I store their positions for later use. Bridge-building BFS : For each cell in the marked island, I run a BFS looking for the second island. Each BFS stops as soon as it hits a cell with value 1 . The minimum distance across all these searches gives the shortest bridge. πŸ” Code Snippet Here's the core logic simplified: public int ShortestBridge(int[][] grid) { // 1. Mark one island as '2' and gather its coordinates List<int> island = FindAndMark...

Classic Dynamic Programming IX

A bit of vibe code together with OpenAI O3. I asked O3 to just generate the sieve due to laziness. Sieve is used to calculate the first M primes (when I was using Miller-Rabin, was giving me TLE). The DP follows from that in a straightforward way: calculate the numbers from i..n-1, then n follows by calculating the min over all M primes. Notice that I made use of Goldbach's Conjecture as a way to optimize the code too. Goldbach's Conjecture estates that any even number greater than 2 is the sum of 2 primes. The conjecture is applied in the highlighted line. Cheers, ACC. PS: the prompt for the sieve was the following, again using Open AI O3 Advanced Reasoning: " give me a sieve to find the first M prime numbers in C#. The code should produce a List<int> with the first M primes " Minimum Number of Primes to Sum to Target - LeetCode You are given two integers  n  and  m . You have to select a multiset of  prime numbers  from the  first   m  pri...