Anagram Detection Practice Problem
This data science coding problem helps you practice String Manipulation, anagram detection, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of String Manipulation.
- Problem ID: 96
- Problem key: 96-anagram-detection
- URL: https://datacrack.app/solve/96-anagram-detection
- Difficulty: easy
- Topic: String Manipulation
- Module: Python Fundamentals
Problem Statement
# 🧩 Anagram Detection
---
### 🎯 Goal
Two strings are **anagrams** if they contain the same characters with the same frequencies, just in a different order. This problem teaches character frequency counting — the same technique used in NLP bag-of-words models, text similarity, and plagiarism detection.
---
### 🔍 What Are Anagrams?
```
"listen" and "silent" → True (same letters, different order)
"hello" and "world" → False (different letters)
"anagram" and "nagaram" → True
```
---
### 💻 Task
Implement `is_anagram(s1, s2)`. Use **sorted strings** for a clean, readable solution.
---
### 📥 Input
- `s1`: string
- `s2`: string
### 📤 Output
- `bool` — `True` if `s1` and `s2` are anagrams, `False` otherwise
---
### 🧩 Starter Code
```python
def is_anagram(s1, s2):
"""
Check if two strings are anagrams of each other.
Args:
s1 (str): First string
s2 (str): Second string
Returns:
bool: True if anagrams, False otherwise
"""
# 🧠 TODO: Two strings are anagrams if sorted(s1) == sorted(s2)
pass
```
---
### 💡 Example
```python
is_anagram("listen", "silent") # Expected: True
is_anagram("hello", "world") # Expected: False
is_anagram("rat", "car") # Expected: False
```
---
### 🔑 Key Concepts
- `sorted(string)` returns a sorted list of characters — same as a sorted character frequency
- If two strings have the same sorted characters, they are anagrams
- Alternative: compare frequency dicts (more efficient for long strings)Starter Code
def is_anagram(s1, s2):
"""
Check if two strings are anagrams of each other.
Args:
s1 (str): First string
s2 (str): Second string
Returns:
bool: True if anagrams, False otherwise
"""
# 🧠 TODO: Two strings are anagrams if sorted(s1) == sorted(s2)
pass