Remove Adjacent Duplicates
Remove adjacent duplicate characters.
AdvancedPhase 5 - Category 5: Advanced String LogicExample 7 of 14
remove-adjacent-duplicates.py
1# Take string input2s = input("Enter a string: ")34# Remove adjacent duplicates5result = ""6prev_char = None7for char in s:8 if char != prev_char:9 result += char10 prev_char = char1112print(f"Result: {result}")
Output
Enter a string: aabbcc Result: abc
What's going on
Track previous character and skip duplicates.
Key Concepts:
Track previous character
Add only if different from previous
Removes adjacent duplicates
🔗Related Content
- 📖
Phase 5 - Learn Concepts
Review Phase 5 concepts and explanations
- 📝
Phase 5 - Quiz
Test your Phase 5 understanding with quiz questions
- 💻
Phase 5 - All Practice Problems
Explore all practice problems for Phase 5
- 🎓
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- 🧠
Logic Building Overview
Learn about the complete logic building curriculum