The Extra Characters in a String coding problem gives you a string s and a dictionary of words. You need to break the string into several non-overlapping substrings such that each substring is either one of the words in the dictionary or is considered "extra characters." Your goal is to minimize the total number of extra characters left over.
Companies like Amazon and Google ask this to evaluate your Dynamic Programming (DP) skills. It’s a variation of the "Word Break" problem. It tests your ability to define a state that minimizes a cost (extra characters) rather than just finding a boolean "is possible." It also evaluates whether you can optimize dictionary lookups using a Hash Set or a Trie.
The problem is solved using 1D Dynamic Programming.
dp[i] as the minimum extra characters for the suffix s[i...].dp[n] = 0 (empty string has 0 extra chars).s[i] as an extra character. dp[i] = 1 + dp[i+1].s[i...]. If s[i...i+len-1] is a valid word, dp[i] = min(dp[i], dp[i+len]).dp[0].s = "leetscode", dictionary = ["leet", "code"]
dp[9] = 0dp[5] = dp[9] = 0.dp[4] = 1 + dp[5] = 1.dp[0] = min(1 + dp[1], dp[4]) = min(..., 1) = 1.
The minimum extra characters is 1 (the 's').Always consider both "Forward DP" (calculating best result for prefix 0..i) and "Backward DP" (suffix i..n). For string partitioning, Backward DP often leads to slightly cleaner base case logic.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Word Break | Medium | Solve | |
| Replace Words | Medium | Solve | |
| Find the Substring With Maximum Cost | Medium | Solve | |
| Find the Length of the Longest Common Prefix | Medium | Solve | |
| Shortest Uncommon Substring in an Array | Medium | Solve |