📖 Scenario: You work in a word game company. You need to check if two words are anagrams. Anagrams are words made by rearranging the letters of another word, like 'listen' and 'silent'.
🎯 Goal: Build a simple program that checks if two given words are anagrams using a dictionary to count letters.
📋 What You'll Learn
Create two string variables with exact given words
Create a dictionary to count letters of the first word
Use a loop to update the dictionary counts
Compare the dictionary counts with the second word
Print True if anagrams, else False
💡 Why This Matters
🌍 Real World
Checking anagrams is useful in word games, puzzles, and text analysis to find related words.
💼 Career
Understanding how to count and compare elements in data structures is a key skill for software developers and data analysts.
Progress0 / 4 steps
1
Create the two words to check
Create two string variables called word1 and word2 with the exact values 'listen' and 'silent' respectively.
DSA Python
Hint
Use simple assignment like word1 = 'listen'.
2
Create a dictionary to count letters in word1
Create an empty dictionary called letter_count to store letter frequencies from word1.
DSA Python
Hint
Use {} to create an empty dictionary.
3
Count letters in word1 using a loop
Use a for loop with variable letter to go through each letter in word1. For each letter, update letter_count by adding 1 if the letter exists, or setting it to 1 if it does not.
DSA Python
Hint
Use if letter in letter_count to check presence, then update count.
4
Check if word2 is an anagram and print result
Create a variable is_anagram and set it to True. Use a for loop with variable letter to check each letter in word2. If a letter is not in letter_count or its count is zero, set is_anagram to False and break the loop. Otherwise, decrease the count of that letter in letter_count by 1. Finally, print is_anagram.
DSA Python
Hint
Use a loop to check letters and print is_anagram at the end.
Practice
(1/5)
1. What does it mean if two strings are anagrams of each other?
easy
A. They contain the same characters in any order
B. They have the same length but different characters
C. They start with the same character
D. They end with the same character
Solution
Step 1: Understand the definition of anagrams
Anagrams are words or phrases made by rearranging the letters of another, so they must have the same characters.
Step 2: Compare the character sets
If two strings have exactly the same characters, just in different orders, they are anagrams.
Final Answer:
They contain the same characters in any order -> Option A
Quick Check:
Anagrams = same characters [OK]
Hint: Check if sorted strings are equal to confirm anagrams [OK]
Common Mistakes:
Thinking same length means anagrams
Only checking first or last character
Ignoring character counts
2. Which of the following Python code snippets correctly checks if two strings s1 and s2 are anagrams using sorting?
easy
A. s1 == s2[::-1]
B. sorted(s1) == sorted(s2)
C. s1.sort() == s2.sort()
D. len(s1) == len(s2)
Solution
Step 1: Understand sorting strings in Python
The sorted() function returns a new sorted list of characters from the string.
Step 2: Compare sorted lists for equality
If sorted(s1) equals sorted(s2), both strings have the same characters in the same counts.
Final Answer:
sorted(s1) == sorted(s2) -> Option B
Quick Check:
sorted strings equal means anagrams [OK]
Hint: Use sorted() to compare strings for anagram check [OK]
Common Mistakes:
Using .sort() on strings (strings have no sort method)