Video Transcription
My name is Art, and I teach Python at Noble Desktop. In this video, I'll walk you through solving one of the most enduring algorithmic challenges in programming: the anagram problem. Anagrams are words or phrases that contain exactly the same letters in the same quantities, just rearranged—think "listen" and "silent," or "evil" and "vile." This problem appears frequently in technical interviews and real-world applications, making it an essential skill for any serious Python developer.
The elegant solution lies in leveraging Python's dictionary data structure to create character frequency maps. We'll start by creating a variable called 'word' and prompting the user for input with something like "Give me a word." The core algorithm uses a for loop to iterate through each letter in the word, building a dictionary where each letter serves as a key and its frequency becomes the corresponding value.
Here's where Python's flexibility shines: if the letter already exists in our dictionary, we increment its count using the += operator. If it's a new letter, we initialize it with a value of 1. When you run this code with a word like "apple," you'll see it generate a dictionary that precisely maps each letter to its frequency: {'a': 1, 'p': 2, 'l': 1, 'e': 1}. This frequency fingerprint is the key to identifying anagrams.
To make this solution scalable and reusable—a fundamental principle in professional software development—we need to encapsulate our logic into a function. Let's call it 'word_to_dictionary' (following Python's snake_case convention). This function takes a word as an argument and returns its character frequency dictionary. This modular approach not only makes your code cleaner but also easier to test and debug.
The comparison logic is beautifully straightforward: we generate dictionaries for both words and compare them directly. Python's dictionary comparison checks both keys and values, so if dictionary_from_word_one equals dictionary_from_word_two, we can confidently declare "Words are anagrams." Otherwise, we output "Words are not anagrams." This approach has O(n) time complexity, making it highly efficient even for longer strings.
This example perfectly illustrates a cornerstone principle of professional programming: when you find yourself repeating code, abstract it into a function. Functions are blocks of reusable code that promote clean architecture, reduce bugs, and make your programs more maintainable. In 2026's competitive development landscape, writing modular, efficient code isn't just good practice—it's essential for career advancement and building robust applications.
Anagrams would be words that have the same count of lettersAnagram Detection Algorithm
Get User Input
Create a variable to store user input and prompt for a word using the input function.
Initialize Dictionary
Create an empty dictionary with curly brackets to store character counts as key-value pairs.
Count Characters
Use a for loop to iterate through each letter, incrementing counts for existing keys or initializing new ones.
Wrap in Function
Convert the logic into a reusable function called 'word_to_dictionary' that takes a word and returns a dictionary.
Compare Dictionaries
Generate dictionaries for both words and compare them directly to determine if they are anagrams.
When you need to perform the same operation multiple times, wrap it in a function. Functions are blocks of reusable code that improve maintainability and reduce duplication.
Dictionary vs List Approach
| Feature | Dictionary Method | List Method |
|---|---|---|
| Time Complexity | O(n) | O(n log n) |
| Memory Usage | Efficient | Higher overhead |
| Code Readability | Clear intent | More complex |
| Implementation | Direct comparison | Sort then compare |
Implementation Checklist
Convert input to lowercase to ensure 'Apple' and 'apple' are treated consistently
Check that inputs are strings and handle empty strings gracefully
Decide whether spaces should be ignored or counted in anagram comparison
Verify behavior with single characters, identical words, and completely different words
Add docstrings explaining what the function does and what it returns
Dictionary-Based Approach
The dictionary approach achieves O(n) time complexity, making it highly efficient even for long strings. This beats alternative sorting-based methods that require O(n log n) time.