String Basic Operations Visualization & Animation
Covers core string primitives — access, concat, substring, search, and split — with their complexity tradeoffs.
## What is it?
String Basic Operations covers the core operations you perform on strings: accessing characters by index, finding substrings, concatenation, splitting, replacing, and checking properties like length or emptiness.
## How it works
Key operations and their typical complexity:
- **Access** character at index `i` → O(1)
- **Length** → O(1) in most languages
- **Concatenation** `s1 + s2` → O(n+m); use `StringBuilder` for repeated concatenation
- **Substring** `s[i:j]` → O(j-i) — creates a copy in most languages
- **Search** (`indexOf`, `find`) → O(n*m) naive, O(n) with KMP
- **Split / Replace** → O(n)
## When to use
- Parsing input (CSV, JSON, commands)
- Building output strings efficiently
- Cleaning and normalizing text data
## Key Points
- Strings are **immutable** in Java, Python, JavaScript — every "modification" creates a new string
- Use `StringBuilder` (Java) or `list.join()` (Python) for O(n) concatenation in loops
- Always handle edge cases: empty string, single character, unicode characters
Category: algorithms
Difficulty: beginner
Time Complexity: O(n)
Space Complexity: O(n)
View String Basic Operations Visualization