24

Phase 6 - Category 2: String + Logic Mix

Chapter 24 • Advanced

105 min

Phase 6 - Category 2: String + Logic Mix

Introduction

This category combines string manipulation with complex logical operations. You'll solve problems requiring both string skills and advanced reasoning.

Key Concepts

String Logic Combinations

  • Anagram Check: Same characters, different order
  • Rotation Check: One string is rotation of another
  • Vowel Analysis: Count vowels per word
  • Character Patterns: Find repeated characters
  • Position-based Logic: Replace based on position

Complex String Operations

  • Reverse Conditional: Reverse words if length is even
  • Toggle Case: Alternate case for words
  • Duplicate Word Removal: Remove repeated words
  • Character Frequency Analysis: Find most frequent characters

Problem-Solving Approach

  1. Combine Techniques: Use string methods with logic
  2. Word-level Processing: Split and process words
  3. Character Analysis: Analyze character patterns
  4. Conditional Transformation: Apply transformations based on conditions

Common Patterns

Anagram Check

python.js
if sorted(str1) == sorted(str2):
    print("Anagrams")

Rotation Check

python.js
if len(str1) == len(str2):
    rotated = str1 + str1
    if str2 in rotated:
        print("Rotation")

Conditional Reverse

python.js
words = text.split()
result = []
for word in words:
    if len(word) % 2 == 0:
        result.append(word[::-1])
    else:
        result.append(word)

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.