Basic String Operations & Their Cost Model
String Traversal
Read aloud is unavailable in this browser.
Walking Through Every Character
If character access is reaching into a string and grabbing a single character, then traversal is systematically walking through the entire string, visiting every character in order. Traversal is the workhorse operation behind nearly every string algorithm:
- Searching for a character or pattern? Traverse the string.
- Counting occurrences? Traverse and count.
- Transforming text? Traverse and build a new string.
- Validating input? Traverse and check each character.
Understanding traversal deeply—not just how to write a loop, but why certain patterns work, when to terminate early, and what the cost implications are—separates competent programmers from those who write inefficient, buggy code.
By the end of this page, you will understand: forward and backward traversal patterns, index-based vs iterator-based traversal, early termination strategies, common traversal idioms, and the O(n) cost model for full traversal.
What Is String Traversal?
Traversal (also called iteration) is the process of visiting each element in a data structure exactly once, in a defined order. For strings, we visit each character from a starting position to an ending position.
The fundamental traversal operation:
Given a string of length n:
- Start at position 0 (or n-1 for reverse)
- Process the character at the current position
- Move to the next position
- Repeat until all positions have been visited
Why "visiting" matters:
Traversal is a read operation—we're observing characters, not necessarily modifying. What we do during each visit (the "processing step") varies by algorithm:
- Counting: Increment a counter if a condition is met
- Searching: Check if the current character matches a target
- Collecting: Add characters to a new string or array
- Validating: Verify the character meets some requirement
- Transforming: Compute a new value based on the character
1234567891011121314
String: "traverse"Length: 8 Forward Traversal Order:Step 1: Visit index 0 → 't'Step 2: Visit index 1 → 'r'Step 3: Visit index 2 → 'a'Step 4: Visit index 3 → 'v'Step 5: Visit index 4 → 'e'Step 6: Visit index 5 → 'r'Step 7: Visit index 6 → 's'Step 8: Visit index 7 → 'e' Total: 8 visits = n visits for a string of length nA complete traversal of a string of length n requires exactly n character accesses. Since each access is O(1), the total time for traversal is O(n). This is optimal—you can't examine every character faster than looking at each one once.
Forward Traversal (Left to Right)
The most common traversal pattern is forward traversal, moving from the first character (index 0) to the last character (index n-1). This matches natural reading order for left-to-right languages and is the default for most string operations.
Index-based forward traversal:
Using an explicit index variable that increments from 0 to n-1:
12345678910111213141516171819202122232425262728
def traverse_forward(s: str) -> None: """ Traverse a string from left to right using index. Time: O(n), Space: O(1) """ for i in range(len(s)): char = s[i] print(f"Index {i}: '{char}'") # Example output for "code": # Index 0: 'c' # Index 1: 'o' # Index 2: 'd' # Index 3: 'e' def count_character(s: str, target: char) -> int: """ Count occurrences of a target character. Demonstrates traversal with accumulation. """ count = 0 for i in range(len(s)): if s[i] == target: count += 1 return count # Example: count_character("mississippi", 's') → 4Iterator-based forward traversal:
Many languages provide a cleaner syntax that abstracts away index management:
12345678910111213141516
def traverse_pythonic(s: str) -> None: """ Pythonic traversal - iterating directly over characters. Cleaner when index isn't needed. """ for char in s: print(char) def traverse_with_enumerate(s: str) -> None: """ When you need both index AND character. enumerate() provides both efficiently. """ for i, char in enumerate(s): print(f"Index {i}: '{char}'")Use index-based traversal when you need the position for calculations (e.g., comparing with other indices). Use iterator-based traversal for cleaner code when only the character values matter. In performance-critical code, both are equivalent—choose for readability.
Backward Traversal (Right to Left)
Sometimes you need to traverse a string in reverse order, from the last character to the first. This is useful for:
- Comparing strings from the end (e.g., checking file extensions)
- Parsing from right to left (e.g., number conversion)
- Finding the last occurrence of something
- Algorithms that need to process characters in reverse
Index-based backward traversal:
12345678910111213141516171819202122232425262728293031
def traverse_backward_index(s: str) -> None: """ Traverse from right to left using index. Time: O(n), Space: O(1) """ for i in range(len(s) - 1, -1, -1): # Start at len(s) - 1 (last index) # Stop before -1 (so 0 is included) # Step by -1 (decrement) print(f"Index {i}: '{s[i]}'") def traverse_backward_pythonic(s: str) -> None: """ Pythonic reverse traversal using reversed(). """ for char in reversed(s): print(char) def find_last(s: str, target: str) -> int: """ Find the last occurrence of a character. Returns -1 if not found. """ for i in range(len(s) - 1, -1, -1): if s[i] == target: return i return -1 # Example: find_last("banana", 'a') → 5Backward traversal is where off-by-one errors love to hide. Remember: start at length - 1 (not length), and continue while i >= 0 (not i > 0, or you'll miss index 0). Always trace through manually with a small example to verify your bounds.
Early Termination: Knowing When to Stop
Full traversal visits every character, but many algorithms don't need to examine the entire string. Early termination means stopping the traversal as soon as you have your answer, avoiding unnecessary work.
Why early termination matters:
For a string of length n:
- Full traversal: Always O(n)
- Early termination: O(1) best case, O(n) worst case
If you're searching for a character and it's at the beginning, why examine the rest?
Examples where early termination applies:
- Searching for first occurrence of a character
- Checking if a string starts/ends with a pattern
- Validating that a string meets a condition (fail fast)
- Finding any element that satisfies a predicate
123456789101112131415161718192021222324252627282930313233343536
def find_first(s: str, target: str) -> int: """ Find the first occurrence of a character. Returns immediately when found (early termination). Best case: O(1) if target is first character Worst case: O(n) if target not present or is last """ for i in range(len(s)): if s[i] == target: return i # STOP: we found it return -1 # Traversed entire string, not found def contains_digit(s: str) -> bool: """ Check if string contains any digit. Early termination on first digit found. """ for char in s: if char.isdigit(): return True # STOP: at least one digit exists return False # No digits found def is_all_lowercase(s: str) -> bool: """ Check if all characters are lowercase. Early termination on first non-lowercase character. This is "fail fast" - we stop at the first violation. """ for char in s: if not char.islower(): return False # STOP: found a violation return True # All characters passed the test- •Return immediately when answer is known
- •Best case can be O(1)
- •Average case often better than O(n)
- •Code clearly shows intent: "found it, done"
- •Always traverses entire string
- •Always O(n) regardless of input
- •Wastes time after answer is known
- •May indicate unclear thinking about the problem
When writing traversal code, always ask: 'Is there a condition under which I already know the answer?' If yes, return immediately when that condition is met. This simple habit improves both performance and code clarity.
Traversal with Collection: Building Results
Many algorithms don't just examine characters—they collect results during traversal. This could mean:
- Building a new string (filtering or transforming)
- Gathering characters into an array or list
- Accumulating statistics (frequencies, positions)
- Constructing a different data structure
The collect pattern:
- Initialize an empty result container
- Traverse the string
- For each character, optionally add to the result
- Return the collected result
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
def filter_vowels(s: str) -> str: """ Remove all vowels from a string. Collects non-vowel characters during traversal. Time: O(n) - visit each character once Space: O(n) - result can be up to n characters """ vowels = set('aeiouAEIOU') result = [] # Use list for efficient append for char in s: if char not in vowels: result.append(char) return ''.join(result) # Example: filter_vowels("hello world") → "hll wrld" def get_character_positions(s: str, target: str) -> list[int]: """ Find ALL positions of a target character. Collects indices during traversal. Time: O(n) Space: O(k) where k = number of occurrences """ positions = [] for i in range(len(s)): if s[i] == target: positions.append(i) return positions # Example: get_character_positions("banana", 'a') → [1, 3, 5] def build_frequency_map(s: str) -> dict: """ Count frequency of each character. Collects statistics during traversal. Time: O(n) Space: O(k) where k = unique characters """ freq = {} for char in s: if char in freq: freq[char] += 1 else: freq[char] = 1 return freq # Example: build_frequency_map("hello") → {'h':1, 'e':1, 'l':2, 'o':1}When collecting during traversal, space complexity depends on what you're collecting. Filtering produces up to O(n) characters. Counting frequencies uses O(k) where k is the number of unique characters. Always analyze both time AND space.
Partial Traversal: Subrange Iteration
Sometimes you need to traverse only a portion of a string—from index start to index end. This is partial traversal, and it's useful when:
- Processing a substring without creating a copy
- Implementing sliding window algorithms
- Comparing sections of different strings
- Working with text that has known regions
1234567891011121314151617181920212223242526272829303132333435363738394041
def traverse_range(s: str, start: int, end: int) -> None: """ Traverse characters from index 'start' to 'end' (exclusive). Parameters: start: First index to visit (inclusive) end: Last index (exclusive), like Python slicing Time: O(end - start) """ for i in range(start, end): print(f"Index {i}: '{s[i]}'") def sum_digits_in_range(s: str, start: int, end: int) -> int: """ Sum all digit characters in a subrange. Example: sum_digits_in_range("abc123def456", 3, 9) processes "123def" → 1+2+3 = 6 """ total = 0 for i in range(start, end): if s[i].isdigit(): total += int(s[i]) return total def compare_ranges(s1: str, start1: int, s2: str, start2: int, length: int) -> bool: """ Compare two substrings without creating actual substring objects. Compares s1[start1:start1+length] with s2[start2:start2+length] Time: O(length) Space: O(1) - no substring allocation """ for i in range(length): if s1[start1 + i] != s2[start2 + i]: return False return TrueCreating a substring to traverse it allocates new memory and copies characters. If you only need to read the characters, traverse the original string with calculated indices. This is a common optimization in performance-sensitive code.
Two-Direction Traversal: Meeting in the Middle
Some algorithms require comparing characters from both ends simultaneously. This two-pointer or two-direction traversal uses two indices moving toward each other.
When to use two-direction traversal:
- Palindrome checking (compare start with end)
- Two-sum type problems on sorted strings
- Partitioning characters (move elements based on condition)
- Reversing a string in place
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
def is_palindrome(s: str) -> bool: """ Check if a string is a palindrome using two-pointer traversal. Time: O(n/2) = O(n) Space: O(1) """ left = 0 right = len(s) - 1 while left < right: if s[left] != s[right]: return False left += 1 right -= 1 return True def is_palindrome_alphanumeric(s: str) -> bool: """ Check palindrome considering only alphanumeric characters. Two pointers skip non-alphanumeric characters. Example: "A man, a plan, a canal: Panama" → True """ left = 0 right = len(s) - 1 while left < right: # Skip non-alphanumeric from left while left < right and not s[left].isalnum(): left += 1 # Skip non-alphanumeric from right while left < right and not s[right].isalnum(): right -= 1 # Compare case-insensitively if s[left].lower() != s[right].lower(): return False left += 1 right -= 1 return True def reverse_string_in_place(chars: list) -> None: """ Reverse a character array in place. (Strings are immutable in many languages, so we use a list) Time: O(n/2) = O(n) Space: O(1) - modifies in place """ left = 0 right = len(chars) - 1 while left < right: # Swap characters chars[left], chars[right] = chars[right], chars[left] left += 1 right -= 1Why this works:
Two-direction traversal is elegant because:
- Each pointer moves only toward the other
- They can never cross (we stop when left >= right)
- Total work is still O(n)—just split between two pointers
- It's perfect for comparing symmetric positions
Visualization:
String: "RACECAR"
↓ ↓
Step 1: R === R ✓ (indices 0 and 6)
↓ ↓
Step 2: A === A ✓ (indices 1 and 5)
↓ ↓
Step 3: C === C ✓ (indices 2 and 4)
↓
Step 4: E (middle - only one pointer, done)
Two-direction traversal is a specific application of the broader 'two-pointer' technique. You'll see this pattern throughout DSA—in arrays, linked lists, and strings. It's a core algorithmic tool worth mastering deeply.
KEEP LEARNING
Keep learning for free.
Read the complete lesson, save your notes and pick up where you left off with a free account. No card needed.