Loading
Loading
Character Arrays with Special Powers
3 of 38 problems completed
Strings are sequences of characters, and string manipulation is one of the most common tasks in programming. Many array techniques apply to strings, but strings also have unique properties and methods.
Strings are immutable in most modern languages (JavaScript, Python, Java). This means every modification creates a new string, which has important performance implications.
String concatenation in a loop is O(nยฒ) due to immutability. Use arrays and join(), or StringBuilder in Java.
// String basics
const str = "hello";
// Access character - O(1)
const char = str[0]; // 'h'
const char2 = str.charAt(0); // 'h'
// Length - O(1)
const len = str.length; // 5
// Immutability - creates new string
const upper = str.toUpperCase(); // "HELLO"
console.log(str); // still "hello"
// Efficient concatenation
const parts = ['a', 'b', 'c'];
const joined = parts.join(''); // "abc"A palindrome reads the same forwards and backwards. Palindrome problems are extremely common in interviews and can be solved using two pointers or dynamic programming.
// Check if string is palindrome
function isPalindrome(s) {
let left = 0;
let right = s.length - 1;
while (left < right) {
if (s[left] !== s[right]) {
return false;
}
left++;
right--;
}
return true;
}
// Expand around center (for finding palindromes)
function expandAroundCenter(s, left, right) {
while (left >= 0 && right < s.length && s[left] === s[right]) {
left--;
right++;
}
return s.slice(left + 1, right);
}Strings cannot be modified in place; operations create new strings
Modern strings use Unicode; some characters take multiple code units
Same as arrays, commonly used for palindromes and reversal
Track character frequencies in a window
const freq = new Map();
let left = 0;
for (let right = 0; right < s.length; right++) {
// Add s[right] to window
while (/* need to shrink */) {
// Remove s[left] from window
left++;
}
}Continue to Math for number theory basics, then Hash Tables for efficient string pattern matching.