DSA Notes
Complete reference for all string operations — reversal, rotation, compression, checking palindrome, counting characters, removing duplicates, replacing characters, converting case, splitting and joining — each with implementation, time and space complexity, and edge cases.
1. Reversal
Reverse Words in a String
3. String Compression
4. Remove Duplicates
def remove_duplicates_keep_order(s):
"""Remove duplicate characters, keep first occurrence. O(n)."""
seen = set()
result = []
for char in s:
if char not in seen:
seen.add(char)
result.append(char)
return ''.join(result)
print(remove_duplicates_keep_order("programming")) # "progamin"5. Character Frequency Count
6. Case Conversion
s = "Hello World"
print(s.upper()) # "HELLO WORLD"
print(s.lower()) # "hello world"
print(s.title()) # "Hello World"
print(s.swapcase()) # "hELLO wORLD"
print(s.capitalize()) # "Hello world"
def toggle_case(s):
"""Toggle case of each character."""
return ''.join(c.lower() if c.isupper() else c.upper() for c in s)
print(toggle_case("HeLLo")) # "hEllO"7. Check Permutation / Anagram
8. String Formatting
name, age, score = "Alice", 25, 98.5
# f-strings (Python 3.6+) — recommended
print(f"Name: {name}, Age: {age}, Score: {score:.1f}")
# "Name: Alice, Age: 25, Score: 98.5"
# Padding and alignment
print(f"{'left':<10}|{'center':^10}|{'right':>10}")
# "left | center | right"
# Number formatting
print(f"{1234567:,}") # "1,234,567"
print(f"{0.1234:.2%}") # "12.34%"
print(f"{255:#010b}") # "0b11111111"9. Splitting and Joining
10. Find and Replace
s = "Hello World Hello"
# Find
print(s.find("Hello")) # 0 (first occurrence)
print(s.rfind("Hello")) # 12 (last occurrence)
print(s.find("xyz")) # -1 (not found)
# Count occurrences
print(s.count("Hello")) # 2
# Replace
print(s.replace("Hello", "Hi")) # "Hi World Hi"
print(s.replace("Hello", "Hi", 1)) # "Hi World Hello" — replace only first
# Strip characters
s = "***hello***"
print(s.strip('*')) # "hello"
print(s.lstrip('*')) # "hello***"Complexity Summary
| Operation | Time | Space | Notes |
|---|---|---|---|
| Access s[i] | O(1) | O(1) | |
| Length len(s) | O(1) | O(1) | Stored |
| Slice s[i:j] | O(j-i) | O(j-i) | Creates new string |
| Concatenate s1+s2 | O(n+m) | O(n+m) | Creates new string |
| Find/search | O(n) | O(1) | Linear scan |
| Replace | O(n) | O(n) | Creates new string |
| Split | O(n) | O(n) | Returns list |
| Join n strings | O(total_length) | O(total_length) | One allocation |
| Upper/lower | O(n) | O(n) | New string |
| Reverse | O(n) | O(n) | New string |
| Count char | O(n) | O(1) |
*Next Lesson: Pattern Searching*
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for String Operations — Complete Reference with Complexity and Implementation.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Data Structures & Algorithms topic.
Search Terms
data-structures-algorithms, data structures & algorithms, data, structures, algorithms, strings, string, operations
Related Data Structures & Algorithms Topics