Episode 7 — DSA with JavaScript / 7.5 — Strings

7.5 — Quick Revision: Strings

<< Overview


String methods cheat sheet (JS)

MethodReturnsMutates?
concat()new stringNo
slice(s,e)substringNo
substring(s,e)substringNo
replace()new string (first match)No
replaceAll()new string (all matches)No
indexOf()index or -1No
includes()booleanNo
startsWith()booleanNo
toUpperCase()new stringNo
trim()new stringNo
split()arrayNo
repeat(n)new stringNo
padStart(n,c)new stringNo

Key algorithms

AlgorithmTimeSpacePattern
ReverseO(n)O(n) or O(1)Two pointers
PalindromeO(n)O(1)Two pointers
Anagram checkO(n)O(1)*Frequency map
Longest unique substrO(n)O(k)Sliding window
LCPO(S)O(1)Vertical scan
KMP matchingO(n+m)O(m)LPS array
Edit distanceO(mn)O(mn)DP table

Common pitfalls

  1. Strings are immutable in JS — methods return new strings
  2. == compares value for primitives, reference for objects
  3. indexOf returns -1 (not false) when not found
  4. Template literals use backticks, not quotes
  5. split("") creates array of chars; split() creates single-element array

Self-check drill

SC-001

  • Q: How to reverse a string?
  • A: split('').reverse().join('') or two pointers

SC-002

  • Q: Palindrome check approach?
  • A: Two pointers from both ends, compare chars

SC-003

  • Q: Anagram check - optimal?
  • A: Frequency map in O(n)

SC-004

  • Q: Are JS strings mutable?
  • A: No — all operations create new strings

SC-005

  • Q: KMP time complexity?
  • A: O(n+m) — linear

SC-006

  • Q: Sliding window for unique chars?
  • A: Set + left/right pointers

SC-007

  • Q: Edit distance approach?
  • A: 2D DP table, compare prefixes

SC-008

  • Q: slice vs substring?
  • A: slice supports negative, substring swaps args

SC-009

  • Q: What does split('') do?
  • A: Creates array of individual characters

SC-010

  • Q: String compression algorithm?
  • A: Run-length encoding: count consecutive chars

SC-011

  • Q: How to reverse a string?
  • A: split('').reverse().join('') or two pointers

SC-012

  • Q: Palindrome check approach?
  • A: Two pointers from both ends, compare chars

SC-013

  • Q: Anagram check - optimal?
  • A: Frequency map in O(n)

SC-014

  • Q: Are JS strings mutable?
  • A: No — all operations create new strings

SC-015

  • Q: KMP time complexity?
  • A: O(n+m) — linear

SC-016

  • Q: Sliding window for unique chars?
  • A: Set + left/right pointers

SC-017

  • Q: Edit distance approach?
  • A: 2D DP table, compare prefixes

SC-018

  • Q: slice vs substring?
  • A: slice supports negative, substring swaps args

SC-019

  • Q: What does split('') do?
  • A: Creates array of individual characters

SC-020

  • Q: String compression algorithm?
  • A: Run-length encoding: count consecutive chars

SC-021

  • Q: How to reverse a string?
  • A: split('').reverse().join('') or two pointers

SC-022

  • Q: Palindrome check approach?
  • A: Two pointers from both ends, compare chars

SC-023

  • Q: Anagram check - optimal?
  • A: Frequency map in O(n)

SC-024

  • Q: Are JS strings mutable?
  • A: No — all operations create new strings

SC-025

  • Q: KMP time complexity?
  • A: O(n+m) — linear

SC-026

  • Q: Sliding window for unique chars?
  • A: Set + left/right pointers

SC-027

  • Q: Edit distance approach?
  • A: 2D DP table, compare prefixes

SC-028

  • Q: slice vs substring?
  • A: slice supports negative, substring swaps args

SC-029

  • Q: What does split('') do?
  • A: Creates array of individual characters

SC-030

  • Q: String compression algorithm?
  • A: Run-length encoding: count consecutive chars

SC-031

  • Q: How to reverse a string?
  • A: split('').reverse().join('') or two pointers

SC-032

  • Q: Palindrome check approach?
  • A: Two pointers from both ends, compare chars

SC-033

  • Q: Anagram check - optimal?
  • A: Frequency map in O(n)

SC-034

  • Q: Are JS strings mutable?
  • A: No — all operations create new strings

SC-035

  • Q: KMP time complexity?
  • A: O(n+m) — linear

SC-036

  • Q: Sliding window for unique chars?
  • A: Set + left/right pointers

SC-037

  • Q: Edit distance approach?
  • A: 2D DP table, compare prefixes

SC-038

  • Q: slice vs substring?
  • A: slice supports negative, substring swaps args

SC-039

  • Q: What does split('') do?
  • A: Creates array of individual characters

SC-040

  • Q: String compression algorithm?
  • A: Run-length encoding: count consecutive chars

SC-041

  • Q: How to reverse a string?
  • A: split('').reverse().join('') or two pointers

SC-042

  • Q: Palindrome check approach?
  • A: Two pointers from both ends, compare chars

SC-043

  • Q: Anagram check - optimal?
  • A: Frequency map in O(n)

SC-044

  • Q: Are JS strings mutable?
  • A: No — all operations create new strings

SC-045

  • Q: KMP time complexity?
  • A: O(n+m) — linear

SC-046

  • Q: Sliding window for unique chars?
  • A: Set + left/right pointers

SC-047

  • Q: Edit distance approach?
  • A: 2D DP table, compare prefixes

SC-048

  • Q: slice vs substring?
  • A: slice supports negative, substring swaps args

SC-049

  • Q: What does split('') do?
  • A: Creates array of individual characters

SC-050

  • Q: String compression algorithm?
  • A: Run-length encoding: count consecutive chars

SC-051

  • Q: How to reverse a string?
  • A: split('').reverse().join('') or two pointers

SC-052

  • Q: Palindrome check approach?
  • A: Two pointers from both ends, compare chars

SC-053

  • Q: Anagram check - optimal?
  • A: Frequency map in O(n)

SC-054

  • Q: Are JS strings mutable?
  • A: No — all operations create new strings

SC-055

  • Q: KMP time complexity?
  • A: O(n+m) — linear

SC-056

  • Q: Sliding window for unique chars?
  • A: Set + left/right pointers

SC-057

  • Q: Edit distance approach?
  • A: 2D DP table, compare prefixes

SC-058

  • Q: slice vs substring?
  • A: slice supports negative, substring swaps args

SC-059

  • Q: What does split('') do?
  • A: Creates array of individual characters

SC-060

  • Q: String compression algorithm?
  • A: Run-length encoding: count consecutive chars

SC-061

  • Q: How to reverse a string?
  • A: split('').reverse().join('') or two pointers

SC-062

  • Q: Palindrome check approach?
  • A: Two pointers from both ends, compare chars

SC-063

  • Q: Anagram check - optimal?
  • A: Frequency map in O(n)

SC-064

  • Q: Are JS strings mutable?
  • A: No — all operations create new strings

SC-065

  • Q: KMP time complexity?
  • A: O(n+m) — linear

SC-066

  • Q: Sliding window for unique chars?
  • A: Set + left/right pointers

SC-067

  • Q: Edit distance approach?
  • A: 2D DP table, compare prefixes

SC-068

  • Q: slice vs substring?
  • A: slice supports negative, substring swaps args

SC-069

  • Q: What does split('') do?
  • A: Creates array of individual characters

SC-070

  • Q: String compression algorithm?
  • A: Run-length encoding: count consecutive chars

SC-071

  • Q: How to reverse a string?
  • A: split('').reverse().join('') or two pointers

SC-072

  • Q: Palindrome check approach?
  • A: Two pointers from both ends, compare chars

SC-073

  • Q: Anagram check - optimal?
  • A: Frequency map in O(n)

SC-074

  • Q: Are JS strings mutable?
  • A: No — all operations create new strings

SC-075

  • Q: KMP time complexity?
  • A: O(n+m) — linear

SC-076

  • Q: Sliding window for unique chars?
  • A: Set + left/right pointers

SC-077

  • Q: Edit distance approach?
  • A: 2D DP table, compare prefixes

SC-078

  • Q: slice vs substring?
  • A: slice supports negative, substring swaps args

SC-079

  • Q: What does split('') do?
  • A: Creates array of individual characters

SC-080

  • Q: String compression algorithm?
  • A: Run-length encoding: count consecutive chars

SC-081

  • Q: How to reverse a string?
  • A: split('').reverse().join('') or two pointers

SC-082

  • Q: Palindrome check approach?
  • A: Two pointers from both ends, compare chars

SC-083

  • Q: Anagram check - optimal?
  • A: Frequency map in O(n)

SC-084

  • Q: Are JS strings mutable?
  • A: No — all operations create new strings

SC-085

  • Q: KMP time complexity?
  • A: O(n+m) — linear

SC-086

  • Q: Sliding window for unique chars?
  • A: Set + left/right pointers

SC-087

  • Q: Edit distance approach?
  • A: 2D DP table, compare prefixes

SC-088

  • Q: slice vs substring?
  • A: slice supports negative, substring swaps args

SC-089

  • Q: What does split('') do?
  • A: Creates array of individual characters

SC-090

  • Q: String compression algorithm?
  • A: Run-length encoding: count consecutive chars

SC-091

  • Q: How to reverse a string?
  • A: split('').reverse().join('') or two pointers

SC-092

  • Q: Palindrome check approach?
  • A: Two pointers from both ends, compare chars

SC-093

  • Q: Anagram check - optimal?
  • A: Frequency map in O(n)

SC-094

  • Q: Are JS strings mutable?
  • A: No — all operations create new strings

SC-095

  • Q: KMP time complexity?
  • A: O(n+m) — linear

SC-096

  • Q: Sliding window for unique chars?
  • A: Set + left/right pointers

SC-097

  • Q: Edit distance approach?
  • A: 2D DP table, compare prefixes

SC-098

  • Q: slice vs substring?
  • A: slice supports negative, substring swaps args

SC-099

  • Q: What does split('') do?
  • A: Creates array of individual characters

SC-100

  • Q: String compression algorithm?
  • A: Run-length encoding: count consecutive chars

SC-101

  • Q: How to reverse a string?
  • A: split('').reverse().join('') or two pointers

SC-102

  • Q: Palindrome check approach?
  • A: Two pointers from both ends, compare chars

SC-103

  • Q: Anagram check - optimal?
  • A: Frequency map in O(n)

SC-104

  • Q: Are JS strings mutable?
  • A: No — all operations create new strings

SC-105

  • Q: KMP time complexity?
  • A: O(n+m) — linear

SC-106

  • Q: Sliding window for unique chars?
  • A: Set + left/right pointers

SC-107

  • Q: Edit distance approach?
  • A: 2D DP table, compare prefixes

SC-108

  • Q: slice vs substring?
  • A: slice supports negative, substring swaps args

SC-109

  • Q: What does split('') do?
  • A: Creates array of individual characters

SC-110

  • Q: String compression algorithm?
  • A: Run-length encoding: count consecutive chars

SC-111

  • Q: How to reverse a string?
  • A: split('').reverse().join('') or two pointers

SC-112

  • Q: Palindrome check approach?
  • A: Two pointers from both ends, compare chars

SC-113

  • Q: Anagram check - optimal?
  • A: Frequency map in O(n)

SC-114

  • Q: Are JS strings mutable?
  • A: No — all operations create new strings

SC-115

  • Q: KMP time complexity?
  • A: O(n+m) — linear

SC-116

  • Q: Sliding window for unique chars?
  • A: Set + left/right pointers

SC-117

  • Q: Edit distance approach?
  • A: 2D DP table, compare prefixes

SC-118

  • Q: slice vs substring?
  • A: slice supports negative, substring swaps args

SC-119

  • Q: What does split('') do?
  • A: Creates array of individual characters

SC-120

  • Q: String compression algorithm?
  • A: Run-length encoding: count consecutive chars

SC-121

  • Q: How to reverse a string?
  • A: split('').reverse().join('') or two pointers

SC-122

  • Q: Palindrome check approach?
  • A: Two pointers from both ends, compare chars

SC-123

  • Q: Anagram check - optimal?
  • A: Frequency map in O(n)

SC-124

  • Q: Are JS strings mutable?
  • A: No — all operations create new strings

SC-125

  • Q: KMP time complexity?
  • A: O(n+m) — linear

SC-126

  • Q: Sliding window for unique chars?
  • A: Set + left/right pointers

SC-127

  • Q: Edit distance approach?
  • A: 2D DP table, compare prefixes

SC-128

  • Q: slice vs substring?
  • A: slice supports negative, substring swaps args

SC-129

  • Q: What does split('') do?
  • A: Creates array of individual characters

SC-130

  • Q: String compression algorithm?
  • A: Run-length encoding: count consecutive chars

SC-131

  • Q: How to reverse a string?
  • A: split('').reverse().join('') or two pointers

SC-132

  • Q: Palindrome check approach?
  • A: Two pointers from both ends, compare chars

SC-133

  • Q: Anagram check - optimal?
  • A: Frequency map in O(n)

SC-134

  • Q: Are JS strings mutable?
  • A: No — all operations create new strings

SC-135

  • Q: KMP time complexity?
  • A: O(n+m) — linear

SC-136

  • Q: Sliding window for unique chars?
  • A: Set + left/right pointers

SC-137

  • Q: Edit distance approach?
  • A: 2D DP table, compare prefixes

SC-138

  • Q: slice vs substring?
  • A: slice supports negative, substring swaps args

SC-139

  • Q: What does split('') do?
  • A: Creates array of individual characters

SC-140

  • Q: String compression algorithm?
  • A: Run-length encoding: count consecutive chars

SC-141

  • Q: How to reverse a string?
  • A: split('').reverse().join('') or two pointers

SC-142

  • Q: Palindrome check approach?
  • A: Two pointers from both ends, compare chars

SC-143

  • Q: Anagram check - optimal?
  • A: Frequency map in O(n)

SC-144

  • Q: Are JS strings mutable?
  • A: No — all operations create new strings

SC-145

  • Q: KMP time complexity?
  • A: O(n+m) — linear

SC-146

  • Q: Sliding window for unique chars?
  • A: Set + left/right pointers

SC-147

  • Q: Edit distance approach?
  • A: 2D DP table, compare prefixes

SC-148

  • Q: slice vs substring?
  • A: slice supports negative, substring swaps args

SC-149

  • Q: What does split('') do?
  • A: Creates array of individual characters

SC-150

  • Q: String compression algorithm?
  • A: Run-length encoding: count consecutive chars

SC-151

  • Q: How to reverse a string?
  • A: split('').reverse().join('') or two pointers

SC-152

  • Q: Palindrome check approach?
  • A: Two pointers from both ends, compare chars

SC-153

  • Q: Anagram check - optimal?
  • A: Frequency map in O(n)

SC-154

  • Q: Are JS strings mutable?
  • A: No — all operations create new strings

SC-155

  • Q: KMP time complexity?
  • A: O(n+m) — linear

SC-156

  • Q: Sliding window for unique chars?
  • A: Set + left/right pointers

SC-157

  • Q: Edit distance approach?
  • A: 2D DP table, compare prefixes

SC-158

  • Q: slice vs substring?
  • A: slice supports negative, substring swaps args

SC-159

  • Q: What does split('') do?
  • A: Creates array of individual characters

SC-160

  • Q: String compression algorithm?
  • A: Run-length encoding: count consecutive chars

SC-161

  • Q: How to reverse a string?
  • A: split('').reverse().join('') or two pointers

SC-162

  • Q: Palindrome check approach?
  • A: Two pointers from both ends, compare chars

SC-163

  • Q: Anagram check - optimal?
  • A: Frequency map in O(n)

SC-164

  • Q: Are JS strings mutable?
  • A: No — all operations create new strings

SC-165

  • Q: KMP time complexity?
  • A: O(n+m) — linear

SC-166

  • Q: Sliding window for unique chars?
  • A: Set + left/right pointers

SC-167

  • Q: Edit distance approach?
  • A: 2D DP table, compare prefixes

SC-168

  • Q: slice vs substring?
  • A: slice supports negative, substring swaps args

SC-169

  • Q: What does split('') do?
  • A: Creates array of individual characters

SC-170

  • Q: String compression algorithm?
  • A: Run-length encoding: count consecutive chars

SC-171

  • Q: How to reverse a string?
  • A: split('').reverse().join('') or two pointers

SC-172

  • Q: Palindrome check approach?
  • A: Two pointers from both ends, compare chars

SC-173

  • Q: Anagram check - optimal?
  • A: Frequency map in O(n)

SC-174

  • Q: Are JS strings mutable?
  • A: No — all operations create new strings

SC-175

  • Q: KMP time complexity?
  • A: O(n+m) — linear

SC-176

  • Q: Sliding window for unique chars?
  • A: Set + left/right pointers

SC-177

  • Q: Edit distance approach?
  • A: 2D DP table, compare prefixes

SC-178

  • Q: slice vs substring?
  • A: slice supports negative, substring swaps args

SC-179

  • Q: What does split('') do?
  • A: Creates array of individual characters

SC-180

  • Q: String compression algorithm?
  • A: Run-length encoding: count consecutive chars

SC-181

  • Q: How to reverse a string?
  • A: split('').reverse().join('') or two pointers

SC-182

  • Q: Palindrome check approach?
  • A: Two pointers from both ends, compare chars

SC-183

  • Q: Anagram check - optimal?
  • A: Frequency map in O(n)

SC-184

  • Q: Are JS strings mutable?
  • A: No — all operations create new strings

SC-185

  • Q: KMP time complexity?
  • A: O(n+m) — linear

SC-186

  • Q: Sliding window for unique chars?
  • A: Set + left/right pointers

SC-187

  • Q: Edit distance approach?
  • A: 2D DP table, compare prefixes

SC-188

  • Q: slice vs substring?
  • A: slice supports negative, substring swaps args

SC-189

  • Q: What does split('') do?
  • A: Creates array of individual characters

SC-190

  • Q: String compression algorithm?
  • A: Run-length encoding: count consecutive chars

SC-191

  • Q: How to reverse a string?
  • A: split('').reverse().join('') or two pointers

SC-192

  • Q: Palindrome check approach?
  • A: Two pointers from both ends, compare chars

SC-193

  • Q: Anagram check - optimal?
  • A: Frequency map in O(n)

SC-194

  • Q: Are JS strings mutable?
  • A: No — all operations create new strings

SC-195

  • Q: KMP time complexity?
  • A: O(n+m) — linear

SC-196

  • Q: Sliding window for unique chars?
  • A: Set + left/right pointers

SC-197

  • Q: Edit distance approach?
  • A: 2D DP table, compare prefixes

SC-198

  • Q: slice vs substring?
  • A: slice supports negative, substring swaps args

SC-199

  • Q: What does split('') do?
  • A: Creates array of individual characters

SC-200

  • Q: String compression algorithm?
  • A: Run-length encoding: count consecutive chars