Strings
Strings
"Strings show up in almost every DSA problem — palindromes, anagrams, parsing — so understanding how they work under the hood is non-negotiable."
Part 1: String Basics and Immutability
A string is a sequence of characters. In JavaScript (and most languages), strings are immutable — once created, you cannot change individual characters. Any "modification" creates a new string.
Creating strings
javascript
let single = 'hello';
let double = "world";
let template = `Hello, ${"Vivek"}!`; // Template literal
// Strings are character arrays under the hood
let s = "hello";
console.log(s[0]); // h
console.log(s[1]); // e
console.log(s.length); // 5
// ❌ You cannot change a character in place
s[0] = "H"; // Silently fails (not an error in JS)
console.log(s); // Still "hello"Iterating over a string
javascript
let word = "hello";
// for loop with index
for (let i = 0; i < word.length; i++) {
console.log(word[i]);
}
// for...of
for (let ch of word) {
console.log(ch);
}
// h e l l oPart 2: Essential String Methods
These are the methods that appear over and over in DSA problems.
Search and access
javascript
let s = "Hello, World!";
console.log(s.length); // 13
console.log(s.charAt(0)); // H
console.log(s.indexOf("World")); // 7 (position of first match)
console.log(s.includes("Hello")); // true
console.log(s.indexOf("xyz")); // -1 (not found)Transforming strings
javascript
let s = " Hello World ";
console.log(s.toUpperCase()); // " HELLO WORLD "
console.log(s.toLowerCase()); // " hello world "
console.log(s.trim()); // "Hello World" (removes whitespace)
let csv = "apple,banana,mango";
console.log(csv.split(",")); // ["apple", "banana", "mango"]
let str = "abcdef";
console.log(str.substring(1, 4)); // "bcd" (index 1 to 3)
console.log(str.slice(-3)); // "def" (last 3 chars)Concatenation and template literals
javascript
let first = "John";
let last = "Doe";
// Old way — concatenation with +
let full1 = first + " " + last;
// Better way — template literals
let full2 = `${first} ${last}`;
console.log(full1); // John Doe
console.log(full2); // John DoePart 3: Converting Between Strings and Arrays
In DSA you often convert a string to an array of characters so you can sort, reverse, or modify it (since strings are immutable).
String ↔ char array
javascript
let s = "hello";
// String to array of characters
let chars = s.split(""); // ["h","e","l","l","o"]
// or
let chars2 = [...s]; // ["h","e","l","l","o"]
// Modify the array (you can't modify a string directly)
chars.reverse(); // ["o","l","l","e","h"]
// Array back to string
let reversed = chars.join("");
console.log(reversed); // "olleh"
// One-liner to reverse a string:
let rev = [...s].reverse().join("");
console.log(rev); // "olleh"Checking for palindrome — a classic DSA pattern
javascript
function isPalindrome(s) {
let reversed = [...s].reverse().join("");
return s === reversed;
}
console.log(isPalindrome("racecar")); // true
console.log(isPalindrome("hello")); // false
// Two-pointer approach (more efficient):
function isPalindromeTP(s) {
let left = 0, right = s.length - 1;
while (left < right) {
if (s[left] !== s[right]) return false;
left++;
right--;
}
return true;
}Building strings efficiently
Concatenating strings in a loop with + is slow in many languages because each + creates a new string. In JavaScript use an array and join; in Java use StringBuilder.
javascript
// ❌ Slow — creates a new string on every iteration
let result = "";
for (let i = 0; i < 100; i++) {
result += i; // inefficient
}
// ✅ Fast — collect parts, join once
let parts = [];
for (let i = 0; i < 100; i++) {
parts.push(i);
}
let result2 = parts.join("");
// Java equivalent: StringBuilder sb = new StringBuilder();
// sb.append(i); → sb.toString();