Roadmap › Stage 9: Greedy & Dynamic Programming › Pattern 45 of 53
DSA pattern guide

DP — Strings

Two strings compared: LCS, edit distance, subsequence counts.

Stage 9 · Greedy & Dynamic Programming8 problems5 Medium · 3 Hard

Use it when

Two strings compared: LCS, edit distance, subsequence counts.

Key idea

dp[i][j] = answer for prefixes s[:i], t[:j]. Match: diagonal. Mismatch: best of the neighbours.

Practice problems, in order

  1. Longest Common SubsequenceMedium
  2. Longest Palindromic SubsequenceMedium
  3. Edit DistanceMedium
  4. Shortest Common SupersequenceMedium
  5. Minimum Insertions to Make PalindromeMedium
  6. Regular Expression MatchingHard
  7. Distinct SubsequencesHard
  8. Palindrome Partitioning IIHard

How to practise this pattern

  1. Read the problem and look for the signal above before writing any code.
  2. Say the brute force out loud, with its time complexity, then ask what work it repeats.
  3. Apply the key idea and write the optimised version yourself.
  4. If you're stuck for more than 10 minutes, take one small hint, not the full solution.
  5. Move to the next problem only when you can explain why this pattern fits.

Stuck on one of these?

The AlgoSolveo extension (free, launching soon) sits beside the LeetCode problem and gives the smallest hint that gets you moving, never the full answer. Solved problems tick off on your roadmap.

Join the extension waitlistTrack it on the roadmap
← Previous patternDP — 2D GridNext pattern →DP — Knapsack