Back to course home
0% completed
Vote For New Content
Extra Characters in a String (medium)
Problem Statement
Given a string s
and an array of words words
. Break string s
into multiple non-overlapping substrings such that each substring should be part of the words.
There are some characters left which are not part of any substring.
Return the minimum number of remaining characters in s
, which are not part of any substring after string break-up.
Examples
-
Example 1:
- Input:
s = "amazingracecar"
,words = ["race", "car"]
- Expected Output:
7
- Justification: The string
s
can be rearranged to form "racecar", leaving 'a', 'm', 'a', 'z', 'i', 'n', 'g' as extra.
- Input:
-
Example 2:
- Input:
s = "bookkeeperreading"
,words = ["keep", "read"]
- Expected Output:
9
- Justification: The words "keep" and "read" can be formed from
s
, but 'b', 'o', 'o', 'k', 'e', 'r', 'i', 'n', 'g' are extra.
- Input:
-
Example 3:
- Input:
s = "thedogbarksatnight"
,words = ["dog", "bark", "night"]
- Expected Output:
6
- Justification: The words "dog", "bark", and "night" can be formed, leaving 't', 'h', 'e', 's', 'a', 't' as extra characters.
- Input:
Constraints:
1 <= str.length <= 50
1 <= dictionary.length <= 50
1 <= dictionary[i].length <= 50
dictionary[i]
ands
consists of only lowercase English lettersdictionary
contains distinct words
Try it yourself
Try solving this question here:
Python3
Python3
. . . .
.....
.....
.....
Like the course? Get enrolled and start learning!
Table of Contents
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible