📖 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 to check if two given words are anagrams using letter counting.
📋 What You'll Learn
Create two string variables with exact words
Create a dictionary to count letters
Write a function to check if two words are anagrams
Print the result as True or False
💡 Why This Matters
🌍 Real World
Checking anagrams is useful in word games, spell checkers, and cryptography.
💼 Career
Understanding dictionaries and string manipulation is important for software developers working with text processing.
Progress0 / 4 steps
1
Create two string variables
Create two string variables called word1 and word2 with values "listen" and "silent" respectively.
DSA C#
Hint
Use string type and assign the exact words.
2
Create a dictionary to count letters
Create a Dictionary<char, int> called letterCount to count letters of word1.
DSA C#
Hint
Use Dictionary<char, int> and initialize it.
3
Write a function to check anagrams
Write a function called AreAnagrams that takes two strings s1 and s2 and returns bool. Use Dictionary<char, int> to count letters in s1 and compare with s2.
DSA C#
Hint
Use two loops: one to count letters in s1, one to subtract counts using s2. Return true if all counts match.
4
Print the result
Use Console.WriteLine to print the result of AreAnagrams(word1, word2).
DSA C#
Hint
Call Console.WriteLine with AreAnagrams(word1, word2) to print the result.
Practice
(1/5)
1. What does it mean if two strings are anagrams of each other?
easy
A. They have the same characters in the same frequency but possibly different order
B. They have the same length but different characters
C. They start with the same letter
D. They contain only vowels
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 with the same counts.
Step 2: Compare the options
Only They have the same characters in the same frequency but possibly different order correctly describes this property; others describe unrelated conditions.
Final Answer:
They have the same characters in the same frequency but possibly different order -> Option A
Quick Check:
Anagram = Same chars & frequency [OK]
Hint: Anagrams have same letters and counts, order doesn't matter [OK]
Common Mistakes:
Thinking same length means anagram
Assuming order must be identical
Confusing anagrams with substrings
2. Which of the following C# code snippets correctly checks if two strings are anagrams by sorting?
easy
A. return s1.Contains(s2);
B. return s1 == s2;
C. return s1.Length == s2.Length;
D. return string.Concat(s1.OrderBy(c => c)) == string.Concat(s2.OrderBy(c => c));
Solution
Step 1: Understand sorting approach for anagrams
Sorting both strings and comparing them checks if they have the same characters in the same order after sorting.
Step 2: Analyze each code snippet
return string.Concat(s1.OrderBy(c => c)) == string.Concat(s2.OrderBy(c => c)); sorts both strings and compares; this is correct. Options B, C, and D do not check anagram conditions properly.