Strings are everywhere in programming โ names, addresses, passwords, file paths, JSON data. Understanding how strings work under the hood makes you a better programmer, even if you rarely implement string algorithms from scratch.
At the most basic level, a string is just a sequence of characters stored as an array of bytes. In most languages, strings are immutable โ once created, you cannot change individual characters. You create new strings by combining or slicing existing ones.
String Comparison
Comparing two strings means going character by character. "abc" comes before "abd" because 'c' is less than 'd'. If one string is a prefix of another, the shorter one comes first. This is called lexicographic (dictionary) order.
The naive comparison is O(min(m, n)) where m and n are the string lengths. Most languages optimize this, but understanding the process helps when you need custom comparisons or are working with very long strings.
String Hashing
String hashing converts a string into a number. The idea is to treat the string as a number in some base. For example, "abc" could be hash = aรpยฒ + bรpยน + cรpโฐ (mod some large prime). This lets you compare strings in O(1) by comparing their hash values.
function hashString(str, base = 31, mod = 1e9 + 7) {
let hash = 0;
for (let i = 0; i < str.length; i++) {
hash = (hash * base + str.charCodeAt(i)) % mod;
}
return hash;
}
console.log(hashString("hello"));
console.log(hashString("hello"));
console.log(hashString("world"));
Hash collisions can happen (two different strings producing the same hash), but with a large prime modulus they are extremely rare. Hashing is the foundation of KMP and Rabin-Karp algorithms we will cover next.
Common String Operations
Most languages give you built-in methods for common tasks: length, substring, find, split, join, replace, and trim. These are O(n) in most cases since they need to copy characters. Understanding the cost helps you avoid unnecessary operations in performance-critical code.
Two-pointer technique is your best friend with strings โ checking palindromes, reversing, removing duplicates. Sliding window is great for finding substrings with certain properties. These patterns come up constantly in interviews and real-world parsing.