String and Scan
What is this?
There is no exotic algorithm here β you just walk through a string or array one time, from start to finish, while carrying along one small piece of information as you go. Maybe it is a running sum, a count, or the best value you have seen so far. The careful part is naming that one quantity precisely and knowing the exact moment to read it off; do that and the answer appears in a single pass.
π‘ Fun fact: Best Time to Buy and Sell Stock is one of the most-asked warm-up questions at big tech companies, yet the trick is almost embarrassingly simple β just remember the cheapest price seen so far. The challenge is resisting the urge to write a slow nested-loop solution.
π The 4 problems in this chapter are free. Sign in with Google or Microsoft to start solving.
Core idea: a huge class of array and string problems needs nothing more than one left-to-right pass while you carry a small running quantity β a length difference, an accumulated shift, a count, or a best-so-far. The answer falls out as you scan; there is no second loop and no extra structure.
The pattern
Each problem walks the input once and maintains a single piece of state that you update at every position. The art is naming that state precisely and knowing exactly when to read it off.
- Single-pass case analysis. When comparing two strings, split on their lengths first, then walk once and allow exactly one mismatch β the case structure does the work that brute force would do with nested loops.
- Suffix or prefix accumulation. When each position is affected by everything after (or before) it, sweep from that end and carry a running sum, so each element's final value is computed in O(1).
- Count per run. When neighbors of the same kind form runs, the answer for each run depends only on its length; tally contributions run by run as you scan.
- Track best-so-far. When you want the optimal pair under an ordering constraint, remember the best left-hand value seen so far and test each new element against it.
The common thread: one variable, one pass, updated at every step.
The problems
One Edit Distance β two strings are one edit apart only if their lengths differ by at most one. Branch on the length case, then scan once: at the first mismatch, either skip one character (insert/delete) or step both (replace) and require the rest to match.
Shifting Letters β each shift applies to a prefix, so the total shift on a position is the sum of all shifts from there onward. Sweep right to left accumulating a running suffix sum, and apply it to each character with a modulo-26 wrap.
Remove Colored Pieces if Both Neighbors are the Same Color β a run of k identical colors yields k - 2 valid moves for that player. Scan, measure each run, and tally each player's moves; compare the totals to decide the winner.
Best Time to Buy and Sell Stock β track the minimum price seen so far; at each day, the best profit is the price minus that running minimum. Keep the best profit across the single pass.
Key takeaways
- Many sequence problems need just one linear pass and one running variable.
- Name your state precisely β length case, suffix sum, run length, or best-so-far β and update it every step.
- Suffix and prefix sums turn "depends on everything after me" into O(1) per element.
- Track a min or best-so-far to solve ordered-pair optimizations without nested loops.
- The instinct to build here β sweep once, carry a little state β underpins sliding windows and prefix-sum chapters later.
Shifting Letters
Core idea: Each entry in
shiftsadvances a whole prefix of the string. So a single character feels every shift that starts at its position or later β its total displacement is the sum ofshiftsfrom its index to the end. Sweep once rightβtoβleft, keep a running suffix sum mod 26, and rotate each letter by it. No prefix is ever touched twice.
1. The problem, in a fresh setting
You operate a wheel-lock combination display β a row of dials, each showing a lowercase letter aβz. Maintenance arrives with a list of service tickets, shifts. Ticket i says: "rotate the first i+1 dials forward by shifts[i] clicks." Forward means aβbββ¦βzβa (the dial wraps).
Tickets are applied in order, and crucially each one only ever touches a prefix β dials 0..i. After every ticket is processed, read off the final letters left to right.
Given the starting string s and the array shifts (same length as s), return the final string.
s |
shifts |
Final string | Why |
|---|---|---|---|
"abc" |
[3, 5, 9] |
"rpl" |
dial 0 feels 3+5+9=17, dial 1 feels 5+9=14, dial 2 feels 9 |
"aaa" |
[1, 2, 3] |
"gdd" |
totals 6, 5, 3 β aβg, aβd, aβd |
"z" |
[1] |
"a" |
single dial, one click, wraps zβa |
" az" |
[2, 0] |
"cz" |
dial 0 feels 2+0=2, dial 1 feels 0 β unchanged |
The earlier a dial sits, the more tickets cover it β so shifts pile up from the right.
Sign in to continue reading
The rest of this lesson is available with a free account. Signing in with Google or Microsoft is free.
Sign in to read the full lesson