String
String problems involve character manipulation, parsing, pattern matching, and transformations. Mastering string algorithms is essential as they appear frequently in interviews and real-world applications.Core Concepts
String Operations
- Access: O(1) in most languages
- Concatenation: O(n) creates new string (immutable)
- Substring: O(n) copies characters
- Comparison: O(n) character by character
Common Techniques
- Two pointers: For in-place modifications
- StringBuilder: Efficient concatenation
- Character array: Mutable operations
- Hash map: Character frequency/mapping
Key Patterns & Techniques
1. Two Pointers
- In-place string reversal
- Palindrome validation
- Remove duplicates
- Partition by criteria
2. Character Mapping
- Frequency counting
- Anagram detection
- Character transformation
- Isomorphic strings
3. State Machine
- String parsing (atoi, calculator)
- Validation (IP address, number)
- Complex format handling
4. String Building
- Avoid repeated concatenation
- Use list/StringBuilder
- Join at the end
Common Approaches
String to Integer (atoi)
Longest Common Prefix
Valid Number
Text Justification
Diagonal Traverse
Problems in This Category
043. Longest Common Prefix
Easy | Frequency: 59.4%Vertical scanning or horizontal scanning for common prefix.
067. String to Integer (atoi)
**Medium” | Frequency: 40.7%State machine for parsing with validation and overflow handling.
097. Text Justification
Hard | Frequency: 32.0%Greedy line filling with space distribution.
058. Valid Number
Hard | Frequency: 47.0%State machine to validate number format including scientific notation.
046. Diagonal Traverse
Medium | Frequency: 56.0%Process matrix diagonals with alternating direction.
047. Missing Ranges
Easy | Frequency: 56.0%Find gaps in sorted array, format as ranges.
095. Goat Latin
Easy | Frequency: 32.0%String transformation with rules for vowels and consonants.
Complexity Characteristics
| Pattern | Time Complexity | Space Complexity | When to Use |
|---|---|---|---|
| Character Scan | O(n) | O(1) | Validation, counting |
| Two Pointers | O(n) | O(1) | In-place modification |
| String Building | O(n) | O(n) | Multiple concatenations |
| State Machine | O(n) | O(1) | Complex parsing |
| Hash Map | O(n) | O(k) | Frequency, mapping |
Interview Tips
Pattern Recognition
- “Parse/validate string” → State machine or character-by-character
- “Transform string” → Build new string, avoid repeated concatenation
- “Common prefix/suffix” → Compare character by character
- “Justify/format text” → Greedy line filling, space distribution
- “Encode/decode” → String building with rules
- “Anagram/permutation” → Frequency map or sort
Common Mistakes
- Repeated string concatenation (use list/StringBuilder)
- Not handling edge cases (empty, single char)
- Off-by-one errors in substring
- Forgetting to handle overflow in atoi
- Not stripping whitespace when needed
- Case sensitivity issues
Key Insights
- Strings are immutable in many languages - concatenation creates new string
- Use character array or list for in-place modifications
- State machine simplifies complex parsing logic
- StringBuilder pattern: collect in list, join at end
- Overflow checking necessary for number parsing
- Edge cases: empty, single character, all same
String Building Efficiency
Bad: Repeated Concatenation
Good: List + Join
Python String Methods
Advanced Patterns
KMP String Matching
Manacher’s Algorithm (Longest Palindrome)
Pro Tip: For string building in loops, always use a list and join at the end instead of repeated concatenation. This changes O(n²) to O(n). For complex parsing, draw a state machine diagram first to clarify the logic.