MASTER YOUR LOGIC BUILDING:Phase 6 - Category 2: String + Logic Mix
Mastering phase 6 - category 2: string + logic mix concepts and implementation.
String + Logic Mix
Why This Matters
The interesting problems live at the intersection: Are these two words anagrams? Is one string a rotation of another? Should I reverse this word only if its length is even? These questions need both string skills and logical thinking. It's like cooking — you combine ingredients (string tools) with a recipe (conditions and loops).
What You'll Learn
- How to check if two strings are anagrams
- How to detect string rotations
- How to apply different rules to different words in one sentence
- How to combine splitting, sorting, reversing, and counting in one solution
Anagrams: Same Letters, Different Order
"listen" and "silent" use the exact same letters. Quick check:
- Normalize both strings (lowercase, remove spaces)
- If lengths differ → not anagrams
- Sort both and compare
str1 = "Listen"
str2 = "Silent"
clean1 = str1.lower().replace(" ", "")
clean2 = str2.lower().replace(" ", "")
if sorted(clean1) == sorted(clean2):
print("Anagrams!")
else:
print("Not anagrams")
Why sorting works: sorting groups identical letters together, so two anagrams produce the same sorted list.
Rotations: One String Shifted Around
"abcde" rotated becomes "cdeab" — the letters wrap around. Clever trick: concatenate the string with itself!
str1 = "abcde"
str2 = "cdeab"
if len(str1) == len(str2) and str2 in (str1 + str1):
print("Rotation!")
If str2 appears inside str1 + str1, it's a rotation. Only works when both strings have the same length.
Conditional Transformations on Words
"Reverse words that have an even number of letters; leave odd-length words alone."
text = "Hello World Programming"
words = text.split()
result = []
for word in words:
if len(word) % 2 == 0:
result.append(word[::-1]) # even → reverse
else:
result.append(word) # odd → keep
print(' '.join(result))
# "olleH dlroW Programming"
Split → apply rule per word → join. This pattern appears again and again.
Mixing Counting with Logic
"Print each word with its vowel count:"
vowels = "aeiouAEIOU"
text = "Python is fun"
for word in text.split():
count = sum(1 for char in word if char in vowels)
print(f"{word}: {count} vowels")
Common Mistakes & Tips
- Comparing anagrams without normalizing — always handle case and spaces first
- Rotation check on different lengths — lengths must match, or the answer is automatically "no"
- Applying the rule to the whole string instead of each word — read carefully: "each word" vs "the string"
- Tip: For anagrams, you can also count letter frequencies with a dictionary — great practice if sorting feels like a shortcut
Practice Focus
- Check if two words are anagrams (ignore case)
- Check if one string is a rotation of another
- Reverse only even-length words in a sentence
- Remove duplicate words while preserving order
- Find the most frequent character in a string
String + logic problems reward careful reading. Identify the string operation first, then wrap it in the right condition!
Hands-on Examples
Check Anagram
# Take two strings
str1 = input("Enter first string: ").lower().replace(" ", "")
str2 = input("Enter second string: ").lower().replace(" ", "")
# Check anagram
if len(str1) != len(str2):
print("Not anagrams (different lengths)")
else:
# Sort both strings and compare
if sorted(str1) == sorted(str2):
print("Anagrams")
else:
print("Not anagrams")Normalize strings (lowercase, remove spaces). If lengths match, sort both strings and compare. If sorted strings are equal, they are anagrams.
Related Page
🔗Related Content
- 💻
Phase 6 - Practice Problems
Practice Phase 6 concepts with hands-on coding problems
- 📝
Phase 6 - Quiz
Test your Phase 6 understanding with assessment questions
- 🎓
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- 🧠
Logic Building Overview
Learn about the complete logic building curriculum