Skip to content

Practice Extra Characters in a String

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#2707
LeetCode Problem

Extra Characters in a String

Problem Statement

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.

Source: Extra Characters in a String on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
s = "leetscode", dictionary = ["leet","code","leetcode"]
Output
1

# Example 2

Input
s = "sayhelloworld", dictionary = ["hello","world"]
Output
3

Constraints

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

Related LeetCode Problems

Practice more problems similar to Extra Characters in a String.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.