Visualized DSA Strategies

Episode 6 Strings

1 h 9 min · JavaScript · Quiz

Title card of Episode 6: Strings

▶ This video is coming to YouTube soon.

The complete guide to strings in JavaScript interviews. First how strings really work: UTF-16 code units, characters are numbers you read with charCodeAt, a string can never change, why += in a loop is O(n²) on paper and how V8's ropes often hide it, why an array of parts and join is the habit that's fast in every engine, what every common method costs, and five traps that catch everyone. Then nine techniques, each worked as a small problem in full: examples, the brute force animated beside the running line of code, the complete code, every line explained, and the better solution the same way.

What you'll learn

  • How a JavaScript string is stored (UTF-16), and what s[i], slice, === and indexOf cost
  • Characters are numbers: charCodeAt minus 97 for letter counts, minus 48 for digits, and String.fromCharCode back
  • Immutability, why += in a loop is O(n²) on paper and when V8 hides it, and the array-of-parts toolbox: push, pop, join
  • Counting letters with an array of 26, and two pointers from both ends
  • Mapping letters both ways with two Maps, and an array used as a stack
  • Adding huge numbers digit by digit, and counting words with split and a Map
  • Expanding palindromes from their centre
  • Finding a pattern with a rolling hash (the idea behind Rabin–Karp)
  • How to spot which technique a problem wants

Chapters

  1. 0:00Intro
  2. 0:24What's a string?
  3. 1:26Characters are numbers
  4. 2:31Character codes worth knowing
  5. 3:21Strings can't change
  6. 4:16Why += in a loop is O(n²), and V8's ropes
  7. 5:17An array: push in place
  8. 6:11The array-of-parts toolbox
  9. 7:17What string methods cost
  10. 7:53Five traps
  11. 8:53Nine string techniques
  12. 9:32Type 1 · Compress runs of letters
  13. 15:51Type 2 · First unique character
  14. 21:37Type 3 · Is it a palindrome?
  15. 27:24Type 4 · Same pattern?
  16. 33:16Type 5 · Remove adjacent duplicates
  17. 39:22Type 6 · Add two number strings
  18. 47:31Type 7 · Most common word
  19. 53:33Type 8 · Longest odd palindrome
  20. 1:00:00Type 9 · Find a word in a text
  21. 1:06:31Spot the technique
  22. 1:07:21Quiz: which technique?
  23. 1:08:08Recap

Quiz: test yourself

The 5 questions from the end of the episode. Pick an answer to see why it's right.