DSA

Strings

Master string operations, pattern matching, and common string manipulation problems for interviews and real-world applications.

By TechCoder TeamLast updated: 2026-06-02
In a Nutshell

Master string operations, pattern matching, and common string manipulation problems for interviews and real-world applications. This hands-on tutorial focuses on practical implementation of strings concepts.

Strings

Strings are sequences of characters. They're fundamental in programming and frequently appear in coding interviews.

String Basics

In Python, strings are immutable - you cannot change characters in-place. Any modification creates a new string.

s = "hello"
s[0] = 'H'  # Error! Strings are immutable

# Instead, create new string
s = 'H' + s[1:]  # "Hello"
PYTHON PLAYGROUND
⏳ Loading editor…

String Operations & Complexity

| Operation | Time Complexity | Example | |-----------|-----------------|---------| | Access | O(1) | s[i] | | Concatenation | O(n+m) | s1 + s2 | | Slice | O(k) | s[i:j] (k = j-i) | | Search | O(n*m) | pattern in string | | Replace | O(n) | s.replace(old, new) |

String Reversal

# Method 1: Slicing (Pythonic)
reversed_str = s[::-1]

# Method 2: Two pointers
def reverse_string(s):
    chars = list(s)  # Convert to list (mutable)
    left, right = 0, len(chars) - 1
    
    while left < right:
        chars[left], chars[right] = chars[right], chars[left]
        left += 1
        right -= 1
    
    return ''.join(chars)
PYTHON PLAYGROUND
⏳ Loading editor…

Pattern Matching

Check if Substring Exists

# Using 'in' operator - O(n*m)
if "pattern" in text:
    print("Found")

# Using find() - returns index or -1
index = text.find("pattern")

Count Occurrences

count = text.count("pattern")
PYTHON PLAYGROUND
⏳ Loading editor…

Anagram Problems

Two strings are anagrams if they contain the same characters with the same frequencies.

def are_anagrams(s1, s2):
    # Method 1: Sort and compare
    return sorted(s1) == sorted(s2)
    
    # Method 2: Count frequencies
    from collections import Counter
    return Counter(s1) == Counter(s2)
PYTHON PLAYGROUND
⏳ Loading editor…

Palindrome Problems

A palindrome reads the same forwards and backwards.

def is_palindrome(s):
    # Simple check
    return s == s[::-1]

def is_palindrome_alphanumeric(s):
    # Ignore non-alphanumeric and case
    cleaned = ''.join(c.lower() for c in s if c.isalnum())
    return cleaned == cleaned[::-1]
PYTHON PLAYGROUND
⏳ Loading editor…

String Compression

def compress_string(s):
    """Compress: 'aaabbcccc' -> 'a3b2c4'"""
    if not s:
        return ""
    
    compressed = []
    count = 1
    
    for i in range(1, len(s)):
        if s[i] == s[i-1]:
            count += 1
        else:
            compressed.append(s[i-1] + str(count))
            count = 1
    
    # Don't forget last group
    compressed.append(s[-1] + str(count))
    
    result = ''.join(compressed)
    return result if len(result) < len(s) else s
PYTHON PLAYGROUND
⏳ Loading editor…

AI Mentor

Confused about "strings string manipulation pattern matching anagrams palindromes string compression"? Ask our AI mentor for a simplified explanation.

Quiz

Quiz

Question 1 of 5

What is the time complexity of checking if a substring exists using 'in'?

O(1)
O(n)
O(n*m)
O(n²)

Key Takeaways

Strings are immutable in Python - modifications create new strings
Pattern matching is O(n*m) for naive search
Anagrams have same characters with same frequencies
Palindromes read the same forwards and backwards
String problems are extremely common in interviews

Master string manipulation for real-world text processing! 🚀