Skip to main content
  1. Algorithms/

String DP

·2 mins· ·
Algorithm Dynamic-Programming String Lcs Palindrome
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Introduction
#

String DP usually falls into two families:

  • prefix DP over one or two strings
  • interval DP over one substring

The state definition must say whether we are matching prefixes, deleting characters, editing source into target, or repairing an interval.

Two-Prefix DP
#

For LCS:

dp[i][j] = LCS length between text1[:i] and text2[:j]

If the current characters match:

dp[i][j] = dp[i - 1][j - 1] + 1

If they do not match:

dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])

Edit Distance
#

For edit distance, the direction matters:

dp[i][j] = minimum operations to convert word1[:i] into word2[:j]

On mismatch, the three operations are:

  • delete from source
  • insert target character
  • replace source character

Delete-Only DP
#

For delete-only problems, replacement and insertion are not legal. On mismatch, choose which side to delete:

1 + min(delete from word1, delete from word2)

Weighted delete problems use character cost instead of unit cost.

Interval Palindrome DP
#

For palindrome subsequence or insertion problems:

dp[left][right] = answer for s[left:right+1]

Fill shorter intervals first. If the ends match, use the inner interval. If they do not, drop or repair one side depending on the problem.

Common Mistakes
#

  • Mixing substring and subsequence.
  • Forgetting that dp[i][j] uses prefix lengths, while characters use i - 1 and j - 1.
  • Mixing insert/delete direction in edit distance.
  • Adding a replace branch to delete-only problems.
  • Filling interval DP in the wrong order.

Related LeetCode#

  • LC 72 Edit Distance
  • LC 97 Interleaving String
  • LC 115 Distinct Subsequences
  • LC 139 Word Break
  • LC 516 Longest Palindromic Subsequence
  • LC 583 Delete Operation for Two Strings
  • LC 712 Minimum ASCII Delete Sum for Two Strings
  • LC 1092 Shortest Common Supersequence
  • LC 1143 Longest Common Subsequence
  • LC 1312 Minimum Insertion Steps to Make a String Palindrome

Related

LeetCode 1312: Minimum Insertion Steps to Make a String Palindrome
·4 mins
LeetCode Hard Dynamic-Programming String Palindrome
LeetCode note for Minimum Insertion Steps to Make a String Palindrome, rebuilt from the original learning note
LeetCode 516: Longest Palindromic Subsequence
·3 mins
LeetCode Medium Dynamic-Programming String Palindrome
LeetCode note for Longest Palindromic Subsequence, rebuilt from the original learning note
Grid DP
·2 mins
Algorithm Dynamic-Programming Grid-Dp
Counting, cost optimization, reverse resource DP, and local geometry
Knapsack DP
·2 mins
Algorithm Dynamic-Programming Knapsack Subset-Sum
0/1, unbounded, reachability, counting, and optimization states
Kadane's Algorithm
·2 mins
Algorithm Kadane Dynamic-Programming Array
Maximum subarray DP with an ending-here invariant
LeetCode 1092: Shortest Common Supersequence
·5 mins
LeetCode Hard Dynamic-Programming String
LeetCode note for Shortest Common Supersequence, rebuilt from the original learning note