Introduction to Strings
Strings are sequences of characters — the building blocks of text processing, user input, file handling, and nearly everything you see on a screen. If arrays are the bread of data structures, strings are the butter.
What is a String?
A string is an ordered sequence of characters. Depending on the language, a string might be:
- An array of characters (C/C++) — literally a
char[]with a null terminator\0 - An immutable object (Python, Java) — once created, its contents cannot be changed
- A dynamic primitive (JavaScript) — primitive type but with object-like methods
In C, strings are null-terminated character arrays. The \0 at the end
tells functions like printf where the string ends. Forgetting it causes
bugs that read garbage memory — a classic source of security
vulnerabilities.
Strings vs. Character Arrays
This distinction trips up many beginners:
| Feature | Character Array (C) | String Object (Python/Java) |
|---|---|---|
| Storage | Contiguous chars + \0 | Internal char array + metadata |
| Mutability | Mutable (can modify in-place) | Immutable (creates new string) |
| Size | Fixed at declaration | Dynamic (grows as needed) |
| Null terminator | Required (\0) | Handled internally |
Immutability trap: In Python and Java, every time you “modify” a string,
you actually create a brand new string. Doing s += "x" in a loop is O(n^2)
total work because each concatenation copies the entire string. Use
"".join(list) in Python or StringBuilder in Java instead.
How Strings Are Stored in Memory
Strings are stored as contiguous blocks of characters, just like arrays. Each character occupies a fixed number of bytes based on the encoding:
| Encoding | Bytes per Char | Character Set |
|---|---|---|
| ASCII | 1 byte | 128 characters (English, digits, symbols) |
| Extended ASCII | 1 byte | 256 characters |
| UTF-8 | 1-4 bytes | All Unicode (1.1M+ characters) |
| UTF-16 | 2-4 bytes | Used internally by Java, JavaScript |
Why encoding matters for interviews: When a problem says “lowercase English letters only,” you know there are exactly 26 possible characters — this lets you use a fixed-size array of 26 instead of a hash map. That constraint is a hint.
Types of String Problems
String problems in interviews generally fall into these categories:
| Category | Techniques | Example Problems |
|---|---|---|
| Traversal | Single/double pointer | Reverse a string, palindrome check |
| Matching | Hash map, KMP, Rabin-Karp | Pattern matching, anagram detection |
| Transformation | Stack, recursion | String to integer, decode ways |
| Substring | Sliding window, DP | Longest substring without repeating |
| Comparison | Sorting, character counting | Anagram groups, common prefix |
Try It Yourself
Type any string and explore how it looks as a character array. Try checking palindromes, reversing, and counting characters:
Strings vs. Arrays — Key Differences
If you just came from the Arrays section, here is what changes with strings:
- Character encoding — arrays hold numbers, strings hold encoded characters
- Immutability — most languages make strings immutable (arrays usually aren’t)
- Built-in methods — strings come with rich libraries (split, join, replace, regex)
- Null termination — C strings need
\0, arrays don’t - Comparison semantics — comparing strings requires character-by-character checks, while arrays of primitives compare values
The two strings above share only some characters at the same positions, but they are anagrams of each other (same characters, different order). Spotting relationships like this is the core of string problem-solving.