You are given a 0-indexed string s and a dictionary of words dictionary. You have to break s into one or more non-overlapping substrings such that each substring is present in dictionary. There may be some extra characters in s which are not present in any of the substrings.
Return the minimum number of extra characters left over if you break up s optimally.
题解
看到最小/最大之类的关键字首先考虑 DP,这题也不例外
假设我们 dp[i] 代表在 i 处的剩下的字符串数量,那么我们递推公式可以这样考虑
当我们不选择 i 的时候,那么第 i + 1 个处的值一定会 +1 那么 dp[i+1] = dp[i] + 1