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

Popular posts from this blog

Advent of Code - Day 6, 2024: BFS and FSM

Golang vs. C#: performance characteristics (simple case study)

Advent of Code - Day 7, 2024: Backtracking and Eval