2755 Extra Characters In A String
2755 Extra Characters In A String
Extra Characters in a String 
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.*
Example 1:
1
2
3
4
5
**Input:** s = "leetscode", dictionary = ["leet","code","leetcode"]
**Output:** 1
**Explanation:** We can break s in two substrings: "leet" from index 0 to 3 and "code" from index 5 to 8. There is only 1 unused character (at index 4), so we return 1.
Example 2:
1
2
3
4
5
**Input:** s = "sayhelloworld", dictionary = ["hello","world"]
**Output:** 3
**Explanation:** We can break s in two substrings: "hello" from index 3 to 7 and "world" from index 8 to 12. The characters at indices 0, 1, 2 are not used in any substring and thus are considered as extra characters. Hence, we return 3.
Constraints:
1
2
3
4
5
1 <= s.length <= 50
1 <= dictionary.length <= 50
1 <= dictionary[i].length <= 50
dictionary[i] and s consists of only lowercase English letters
dictionary contains distinct words
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution:
def minExtraChar(self, s: str, dictionary: List[str]) -> int:
n, dictionary_set = len(s), set(dictionary)
@cache
def dp(start):
if start == n:
return 0
# To count this character as a left over character
# move to index 'start + 1'
ans = dp(start + 1) + 1
for end in range(start, n):
curr = s[start: end + 1]
if curr in dictionary_set:
ans = min(ans, dp(end + 1))
return ans
return dp(0)
This post is licensed under CC BY 4.0 by the author.