Strings
String Operations & Manipulation
Master essential string manipulations: reversals, substring extraction, character frequency counting, StringBuilder, and anagram validation.
Last Updated: August 2, 2026
•
20 min read
1. Introduction
What are String Operations?
String Operations encompass core manipulation tasks: extracting substrings, reversing strings, comparing text sequences, checking for anagrams/palindromes, and building composite strings efficiently.Why is it Important?
String problems represent over 25% of all technical coding interviews. Knowing when to use a mutable builder vs character frequency array vs sorting determines whether your solution runs inO(N) or O(N²) time.
Where is it Used?
2. Mental Model
Imagine building a Lego tower out of individual letter blocks.
If you assemble the tower by gluing one block at a time (immutability), every addition requires buying a whole new table setup. If you use a mutable buildertray (StringBuilder / array of chars), you append blocks in O(1) time, snapping the final tower together only when complete!
3. Concept: Core String Manipulation Patterns
1. In-Place String Reversal & Substrings
Reversing a string is performed by converting to a character array and swapping characters from both ends (O(N) time, O(1) space).2. Valid Anagram Check (Frequency Array vs Sorting)
Two strings are anagrams if they contain the exact same character frequencies.O(N log N) time, O(1) or O(N) space).O(N) time, O(1) space).3. Mutable String Building (StringBuilder)
4. Visuals
Anagram Frequency Counter Flowchart
5. Real-World Examples
6. Interview Perspective
How Interviewers Ask This Topic
Interviewers present problems like "Group Anagrams" or "Reverse Words in a String" to evaluate whether you manage memory and string builders efficiently.Common Mistakes
Warning: 1. Substring Extraction in Loops:
s.substring(i, j) creates a copy of length J - I. Doing this repeatedly inside nested loops leads to hidden O(N³) time complexity.> 2. Ignoring Non-Alphabet Characters: Assuming strings only contain 'a'-'z' when problem statements mention full ASCII/Unicode. Use a size-256 array or HashMap for full ASCII.
7. Summary
count[c - 'a'] provides O(1) space character counting for lowercase English.StringBuilder in Java or ''.join() in Python to avoid O(N²) concatenation costs.O(N) time instead of sorting in O(N log N).8. Quiz
Question 1: What is the time complexity of checking if two strings of length N are anagrams by sorting them?
Answer:O(N log N) time due to string sorting algorithms (QuickSort / MergeSort).
Question 2: What is the optimal time and space complexity for checking valid anagrams using a frequency array?
Answer:O(N) time and O(1) auxiliary space (fixed size-26 integer array).
Question 3: Why is StringBuilder append() O(1) amortized while String += is O(N)?
Answer:StringBuilder uses a dynamic character array buffer that resizes by doubling. String += creates a brand new string object and copies all characters every time.
Question 4: What is the space complexity of reversing a string of length N in C++?
Answer:O(1) space, because std::string is mutable and elements can be swapped in-place.
Question 5: How do you group anagrams from a list of N words of max length L using a HashMap?
Answer: Compute a character count tuple (size 26) as the HashMap key for each word. Map key\to list of matching words in O(N × L) total time.