LCS and Edit Distance

You know LIS with 1D states. Now work with two sequences simultaneously. LCS and Edit Distance are the basis of all string comparison algorithms.

41 lessons
148 min
Codeforces: 1400-2000LeetCode: 1600-2000

Lessons

1. Intro

The Goal

4m

2. Two-Dimensional State

Two sequences

4m

3. LeetCode 1143 Longest Common Subsequence - Problem Statement

LeetCode 1143

3m1 problems

4. LeetCode 1143 Longest Common Subsequence - State Design

The 2D table

3m

5. LeetCode 1143 Longest Common Subsequence - Transition

Match or skip

3m

6. LeetCode 1143 Longest Common Subsequence - Implementation

The code

4m1 problems

7. LeetCode 1143 Longest Common Subsequence - Walkthrough

Tracing the algorithm

4m

8. Quiz: LCS Concepts

Knowledge check

3m1 problems

9. LeetCode 1143 Longest Common Subsequence - Reconstruction

Finding the actual sequence

3m

10. Lessons from LCS

summary

4m

11. Challenge: LCS Space Optimization

Reducing memory

4m

12. LeetCode 712 Minimum ASCII Delete Sum for Two Strings - Problem Statement

LeetCode 712

3m1 problems

13. LeetCode 712 Minimum ASCII Delete Sum for Two Strings - Weighted DP

Cost instead of count

3m

14. LeetCode 712 Minimum ASCII Delete Sum for Two Strings - Implementation

The code

4m1 problems

15. LeetCode 712 Minimum ASCII Delete Sum for Two Strings - Walkthrough

Weighted LCS variant

4m

16. LeetCode 72 Edit Distance - Problem Statement

LeetCode 72

3m1 problems

17. LeetCode 72 Edit Distance - Operations

Three choices

3m

18. LeetCode 72 Edit Distance - State Design

Minimum cost

3m

19. LeetCode 72 Edit Distance - Transition

The recurrence

3m

20. LeetCode 72 Edit Distance - Implementation

The code

4m1 problems

21. LeetCode 72 Edit Distance - Walkthrough

Tracing transformations

4m

22. Challenge: Edit Distance Reconstruction

Finding the operations

4m

23. Quiz: Edit Distance

Knowledge check

3m1 problems

24. Lessons from Edit Distance

summary

4m

25. Pattern - String DP

Recognition tips

3m

26. LeetCode 516 Longest Palindromic Subsequence - Problem Statement

LeetCode 516

4m1 problems

27. LeetCode 516 Longest Palindromic Subsequence - Walkthrough

LCS with itself

4m

28. LeetCode 583 Delete Operation for Two Strings - Problem Statement

LeetCode 583

4m1 problems

29. LeetCode 115 Distinct Subsequences - Problem Statement

LeetCode 115

4m1 problems

30. LeetCode 115 Distinct Subsequences - Walkthrough

Counting occurrences

4m

31. Quiz: String DP Patterns

Knowledge check

3m1 problems

32. LeetCode 1092 Shortest Common Supersequence - Problem Statement

LeetCode 1092

3m1 problems

33. LeetCode 1092 Shortest Common Supersequence - The LCS Connection

Length formula

3m

34. LeetCode 1092 Shortest Common Supersequence - Implementation

The code

5m1 problems

35. LeetCode 1092 Shortest Common Supersequence - Walkthrough

Building from LCS

4m

36. LeetCode 44 Wildcard Matching - Problem Statement

LeetCode 44

3m1 problems

37. LeetCode 44 Wildcard Matching - State Design

Boolean 2D DP

3m

38. LeetCode 44 Wildcard Matching - Implementation

The code

4m1 problems

39. LeetCode 44 Wildcard Matching - Walkthrough

Pattern matching DP

4m

40. What's Next

Preview of Interval DP

4m

41. Section Recap

What we learned

5m

Practice Problems

1.

The canonical LCS problem - master this first.

2.
Edit DistanceLeetCodehard

Classic Levenshtein distance with insert/delete/replace operations.

3.
LCS (DP Contest F)AtCodermedium

Requires reconstructing the actual LCS string, not just its length.

4.

LCS of string with its reverse - elegant problem transformation.

5.

Counts ways to form subsequence - extends LCS to combinatorics.

6.

Covered with full walkthrough in this section.

7.

Covered with full walkthrough in this section.

8.

Covered with full walkthrough in this section.

9.

Covered with full walkthrough in this section.

10.
Catching CheatersCodeforcesmedium

Modified LCS scoring. Teaches adapting classic DP to new objective functions.

11.
Changing a StringCodeforcesmedium

Edit distance with operation reconstruction. Must output the actual edit sequence.

12.
Sequence of BallsCodeforceshard

Transform sequences with weighted operations. Generalized edit distance with costs.

13.
Interleaving StringLeetCodemedium

Check if s3 interleaves s1 and s2. 2D DP tracking positions in both strings.

14.

Pattern matching with . and *. The hardest string matching DP - interview classic.

15.
Flood FillCodeforcesmedium

Minimum operations to make array uniform. Interval DP on strings.

16.
Erase SubsequencesCodeforceshard

Check if string can be formed from two disjoint subsequences. DP on two pointers.

17.
Even PictureCodeforcesmedium

Construct grid with string constraints. Pattern-based construction.

18.
TetrahedronCodeforcesmedium

Count paths on tetrahedron. Matrix DP with state transitions.

19.
Gourmet ChoiceCodeforcesmedium

Assign values satisfying comparison constraints. Graph + DP approach.

20.
Three ReligionsCodeforceshard

Check if 3 strings are disjoint subsequences. 3D DP with online updates.

21.
World of DarkraftCodeforceshard

Maximize profit with weapon/armor constraints. Segment tree + DP.

22.
ZigzagsCodeforcesmedium

Count quadruples with equal endpoints. Prefix counting + DP.

Ready to start learning?

Access all 41 lessons with interactive content and progress tracking.