The Day I Realized I Was a Noob Dev: Lessons from an Anagram Mistake

KagemaNjoroge - Feb 22 - - Dev Community

Every programmer has their moments of humility, and I had mine on a day when I thought I had it all figured out. It all started with a seemingly simple programming problem - checking if two strings were anagrams. Little did I know that this challenge would reveal just how much I had to learn.
The Problem That Fooled Me
The problem was straightforward: Given two strings, s and t, determine if t is an anagram of s.
An anagram, as we all know, is a word or phrase formed by rearranging the letters of another word or phrase, typically using all the original letters exactly once. Piece of cake, right?

Here's the question in its entirety:
LeetCode link to the problem

'''
Given two strings s and t, return true if t is an anagram of s, 
and false otherwise.
An Anagram is a word or phrase formed by rearranging the letters
of a different word or phrase, typically using all the original
letters exactly once.

Example 1:

Input: s = "anagram", t = "nagaram"
Output: true
Example 2:

Input: s = "rat", t = "car"
Output: false


Constraints:

1 <= s.length, t.length <= 5 * 104
s and t consist of lowercase English letters.

'''
Enter fullscreen mode Exit fullscreen mode

My Initial Solution
I dived into the problem headfirst, convinced that I could conquer it effortlessly. My initial solution? Compare the two strings character by character and return True if they were the same length. Simple enough, or so I thought:

def anagram(s: str, t: str) -> bool:
    if len(s) != len(t):
        return False

    data_t = {}
    data_s = {}

    for i in s:
        data_s[i] = i
    for f in t:
        data_t[f] = f

    return data_s == data_t
Enter fullscreen mode Exit fullscreen mode

The "Aha! 😒" Moment

I ran my code, and it seemed to work for some test cases. I felt a sense of accomplishment. But then reality hit me like a ton of bricks. What about the characters' counts? An anagram isn't just about having the same characters; it's about having the same characters in the same quantities.

The Big Mistake

My "brilliant" solution had completely overlooked this crucial aspect. I realized that my code would incorrectly declare strings like "rat" and "car" as anagrams simply because they contained the same characters.

LeetCode anagram challenge

I had rushed into solving the problem without fully understanding its requirements. But there's no shame in making mistakes; the real shame is not learning from them.
The Revised Solution

I knew I had to fix my code. I learned about the importance of character counts in anagrams and came up with a revised solution that properly addressed this aspect.

def anagram(s: str, t: str) -> bool:
    if len(s) != len(t):
        return False

    char_count = {}

    for char in s:
        if char in char_count:
            char_count[char] += 1
        else:
            char_count[char] = 1

    for char in t:
        if char in char_count:
            char_count[char] -= 1
            if char_count[char] < 0:
                return False
        else:
            return False

    return True
Enter fullscreen mode Exit fullscreen mode

Lesson
never underestimate the importance of understanding the problem fully

Go ahead and tell me about the day you knew that you had to work on you programming skills.

. . . . . . . . . . . . . . . .