Given a string s and a collection of distinct words in dictionary, split s into consecutive, non-overlapping pieces. A piece that exactly matches a word in dictionary contributes no extra characters; every other character contributes one extra character. Dictionary words may be used wherever they match, including more than once.
The order of characters in s cannot change, and every character must belong to exactly one chosen piece. Return the smallest possible number of extra characters after choosing the best valid collection of dictionary-word matches.
Implement Solution.minExtraChar(self, s: str, dictionary: List[str]) -> int and return that minimum as an integer.
Example 1:
Input: s = "blueberry", dictionary = ["blue","berry","red"]
Output: 0
Explanation: Use "blue" for the first four characters and "berry" for the remaining five. Every character is covered by a dictionary word, so the number of extra characters is 0.
Example 2:
Input: s = "caterpillar", dictionary = ["cat","art","pillar"]
Output: 2
Explanation: Use "cat" for the first three characters and "pillar" for the final six characters. The two characters between them, "er", are not covered by a dictionary word, so both count as extra.
Example 3:
Input: s = "qwerty", dictionary = ["we","ert"]
Output: 3
Explanation: Using "ert" covers the third through fifth characters, leaving "q", "w", and "y" extra. Alternatively, using "we" leaves "q", "r", "t", and "y" extra, so the better choice has 3 extra characters.
Constraints:
1 <= len(s) <= 501 <= len(dictionary) <= 10001 <= len(dictionary[i]) <= 50s and every dictionary word contain only lowercase English lettersdictionary contains distinct wordsNeed help or want more practice?