Extra Characters in a String
StepbyStep Approach
Initialize a Dynamic Programming (DP) Array: Create a DP array of length
s.length + 1
, initialized with maximum integer values except fordp[0]
which is 0.Sort the Dictionary: Sort the words in
dictionary
by length to consider smaller words first. It will facilitate faster lookups.DP Population: Iterate through the DP array, starting from index 1. At each index
i
, iterate through all the dictionary words and check if a substring ending ati
matches any of the words. Updatedp[i]
based on the minimal leftover characters if the substring is a valid dictionary word.Find Minimum Leftovers: The
dp[s.length]
will contain the minimal number of leftover characters.
Effects of Parameter Changes
 Increasing the
s.length
will make the problem harder to solve. The time complexity will rise.  Increasing the number of words in
dictionary
will also make lookups more timeconsuming.
Example Cases
Example 1: “leetscode”, [“leet”,“code”,“leetcode”]
 At
dp[4]
(index starts from 1),dp[4] = min(dp[4], dp[0] + 0)
because “leet” is a valid word.  At
dp[9]
,dp[9] = min(dp[9], dp[4] + 1)
because “code” is a valid word, but has 1 leftover character ’s’ before it.  Result:
dp[9] = 1
 At
Example 2: “sayhelloworld”, [“hello”,“world”]
 At
dp[8]
,dp[8] = min(dp[8], dp[3] + 3)
because “hello” is a valid word, but has 3 leftover characters “say” before it.  At
dp[13]
,dp[13] = min(dp[13], dp[8] + 0)
because “world” is a valid word.  Result:
dp[13] = 3
 At
Key Terms
 Dynamic Programming: DP helps to break down the problem into subproblems and stores the result to avoid redundant calculations.
 Substring: A contiguous sequence of characters within a string.
Your approach would be to use Dynamic Programming to minimize the number of leftover characters while iterating through each index of the string s
. At each index, you’ll check if there’s a substring ending there that exists in the dictionary
and if so, you’ll update your DP array accordingly.
Define Problem
 Problem to be Solved: The goal is to break a given string
s
into one or more nonoverlapping substrings such that each substring exists in a given dictionary. The aim is to minimize the number of leftover characters.  Precondition: We have a string
s
and a dictionary of words. Both are nonempty and contain only lowercase English letters.  Postcondition: We return the minimum number of leftover characters after optimally breaking the string.
Define Step
 Initialize a DP array to track minimal leftovers at each index.
 Iterate over the string
s
to populate the DP array.
Measure of Progress
The DP array gets updated with the minimal number of leftover characters at each index as we iterate through the string s
.
Define Loop Invariant
At the start of each main loop iteration for index i
, dp[j]
for all j < i
contains the minimum number of leftover characters when s
is broken optimally up to index j
.
Main Steps
for i from 1 to len(s):
for word in dictionary:
end = i
start = i  len(word)
if start >= 0 and s[start:end] == word:
dp[i] = min(dp[i], dp[start] + (i  start  len(word)))
Make Progress
Each iteration updates dp[i]
with the minimum number of leftovers when the string is broken up to index i
.
Maintain Loop Invariant
The loop updates dp[i]
for each valid word ending at i
, ensuring the loop invariant holds true for the next iteration.
Establish the Loop Invariant
Initialize dp array with max integer values
dp[0] = 0
Exit Condition
The loop terminates when i
reaches len(s)
.
Ending
 The exit condition ensures that
dp[len(s)]
will have the minimum number of leftover characters after optimally breaking the string, thus solving the problem.  The required output is
dp[len(s)]
.
return dp[len(s)]
This pseudocode captures the essence of solving the problem while preserving the loop invariants and establishing the preconditions and postconditions.
We’ll solve this problem using a dynamic programming approach. The idea is to use a DP array to keep track of the minimum number of extra characters at each index.
Here’s the code for the function minExtraChar
:


Explanation:
Initialization: We initialize the
dp
array of lengthn + 1
withfloat('inf')
. This array will keep track of the minimum extra characters at each index, starting from 0.Iterate through the string: We iterate through the string
s
using indexi
.Check for words: For every word in the dictionary, we check if it starts from the current index
i
. If it does, we update thedp
array for that word length.Update Extra Characters: If no word starts from the current index
i
, then the current character is considered extra, and we update thedp
array accordingly.Final Result:
dp[n]
will hold the minimum extra characters, and we return it.
This algorithm runs in ( O(n \cdot m \cdot k) ) time, where ( n ) is the length of the string s
, ( m ) is the number of words in the dictionary, and ( k ) is the average length of the words in the dictionary.
Identifying Problem Isomorphism
“Extra Characters in a String” can be mapped to “Word Break II”. Both problems involve breaking a string into substrings that are words in a given dictionary.
In “Word Break II”, you are given a nonempty string and a dictionary wordDict containing a list of nonempty words, and you are asked to add spaces in s to construct a sentence where each word is a valid dictionary word.
The similarity is in the task of breaking a string into valid words from a dictionary. However, the goals are slightly different. In “Word Break II”, you aim to find all possible sentences, while in “Extra Characters in a String”, you aim to minimize the number of unused characters.
To adapt “Word Break II” to “Extra Characters in a String”, you would need to modify the approach to keep track of unused characters. Rather than finding all valid sentences, you could use a similar dynamic programming approach to find the division of the string that leaves the fewest characters unused.
10 Prerequisite LeetCode Problems
Before you tackle the problem “2707. Extra Characters in a String”, here are some easier problems involving string manipulation and dynamic programming:
Word Break (LeetCode 139): This is a similar problem where you need to break a string into words given in a dictionary. This problem doesn’t involve extra characters but lays the foundation for the given problem.
Valid Anagram (LeetCode 242): This problem involves comparing two strings and can be solved using a hash map. This helps you understand the basics of string comparison.
Longest Substring Without Repeating Characters (LeetCode 3): This problem involves finding substrings without repeating characters. It gives an understanding of substrings and their manipulations.
Ransom Note (LeetCode 383): This problem is about determining if one string can be constructed from another string. This will help you understand how characters can be used from a string.
Word Pattern (LeetCode 290): This problem helps you understand how strings can follow a certain pattern and how to determine the pattern.
First Unique Character in a String (LeetCode 387): This problem is about finding unique characters in a string. This problem helps you understand how to handle characters in a string.
Longest Palindromic Substring (LeetCode 5): This problem is about finding substrings and can be solved using dynamic programming. It will provide you with an understanding of how to find substrings optimally.
Repeated Substring Pattern (LeetCode 459): This problem is about identifying a repeating pattern in a string. It helps you understand how to recognize patterns in a string.
Add Binary (LeetCode 67): This problem is about binary addition of two strings. It gives a good understanding of string manipulations.
Reverse Words in a String III (LeetCode 557): This problem is about reversing words in a string. This problem gives you an understanding of how to handle words in a string.
These cover string manipulation, substring identification, and dynamic programming.




Problem Classification
The problem fits into the category of String Manipulation and Dynamic Programming. This is because it involves manipulating and processing a string to form substrings, and it requires a strategy that optimizes for a particular conditionâ€”in this case, the minimization of leftover characters. The problem also has overlapping subproblems, a key feature of dynamic programming problems.
‘What’ Components
 String s: A string that needs to be broken down into substrings such that each substring is a word from the dictionary.
 Dictionary of words: A set of allowed words which the substrings in s can represent.
 Nonoverlapping substrings: Each character in the string s can only belong to a single substring.
 Leftover characters: Any characters that are not part of the substrings forming dictionary words.
 Optimal break: The break up of string s into substrings such that the number of leftover characters is minimized.
This problem is an Optimization Problem, specifically a Minimization Problem. The goal is to minimize the number of leftover characters after breaking the string into dictionary words. We want the best configuration that yields the smallest number of remaining characters.
Due to the nature of breaking down the problem into smaller subproblems that overlap, this is also a Dynamic Programming Problem. We can break down the string into smaller substrings and utilize the solutions to these smaller subproblems to build up the solution to the larger problem.
The problem also involves working with String Manipulation and Processing, as the task involves manipulating and breaking down a string based on certain conditions.
Language Agnostic Coding Drills
Dissection of Code
The code can be dissected into the following individual coding concepts:
a. Defining a Function: A function
func
andminExtraChar
are defined.b. Conditionals: The
if
statement is used to make decisions based on whether certain conditions are met.c. Loops: A
for
loop is used to iterate over a sequence of elements (in this case, fromidx
tolen(s)
).d. List Comprehension: The statement
dp = [1] * (len(s) + 1)
creates a list using list comprehension.e. Use of Set: The set
st
is used to contain the dictionary words for efficient lookup.f. Substrings: The operation
s[idx:j + 1]
extracts a substring from the main string.g. Dynamic Programming: The concept of dynamic programming is used with the
dp
list storing intermediate results.h. Recursion: The
func
function is called recursively to solve subproblems.List of Coding Concepts (Drills) in Order of Increasing Difficulty
a. Defining a Function (Easy): This is a basic concept where a function is defined to perform a task.
b. Conditionals (Easy): The
if
statement, used for decisionmaking based on a condition.c. Loops (Intermediate): Using
for
loop to iterate over a range of values.d. List Comprehension (Intermediate): To generate a list in a more pythonic and compact way.
e. Use of Set (Intermediate): The usage of set for efficient lookup.
f. Substrings (Intermediate): Extracting substrings from a string is an important part of string manipulation.
g. Recursion (Hard): Understanding recursion requires understanding the stack structure and is generally considered harder for beginners.
h. Dynamic Programming (Hard): This concept requires a solid understanding of both recursion and optimization techniques to effectively solve a problem by breaking it down into smaller subproblems and storing the solutions to these subproblems.
Problemsolving Approach
To solve this problem, the code follows a dynamic programming approach along with recursion.
a. Initialization: First, the code initializes a list
dp
to store the minimum number of extra characters for each prefix of the strings
. All elements indp
are initially set to1
, indicating that no computation has been done yet.b. Set Creation: It then creates a set
st
from thedictionary
for efficient lookup of words.c. Recursion and Dynamic Programming: The function
func
takes the starting indexidx
of a substring, the strings
, the setst
, and the listdp
as input. It checks whether the current indexidx
has reached the end ofs
. If it has, it returns0
, since no extra characters are left.d. Loop over Substrings and Recursion: The function then loops from
idx
to the end ofs
, checking each substrings[idx:j + 1]
. If the substring is inst
, it recursively callsfunc
for the remaining substrings[j + 1:]
with no extra characters. Otherwise, it callsfunc
for the remaining substring withj  idx + 1
extra characters. The minimum result is stored indp[idx]
for future reference.e. Final Output: The function
minExtraChar
finally returns the minimum number of extra characters for the entire string, which isdp[0]
. This is achieved by the functionfunc
being called withidx
set to0
.
Each of these steps builds upon the previous steps to create a comprehensive solution to the problem. The use of recursion and dynamic programming helps optimize the solution by avoiding duplicate computations.
Targeted Drills in Python
Python Drills for Each Concept:
a. Defining a Function
1 2 3
def hello_world(): return "Hello, world!" print(hello_world())
b. Conditionals
1 2 3 4 5 6 7
x = 10 if x > 0: print("x is positive") elif x < 0: print("x is negative") else: print("x is zero")
c. Loops
1 2
for i in range(5): print(i)
d. List Comprehension
1 2
squares = [x ** 2 for x in range(6)] print(squares)
e. Use of Set
1 2
my_set = set([1, 2, 2, 3, 4, 4, 4, 5, 6]) print(my_set) # Outputs: {1, 2, 3, 4, 5, 6}
f. Substrings
1 2
s = "Hello, world!" print(s[0:5]) # Outputs: "Hello"
g. Recursion
1 2 3 4 5 6
def factorial(n): if n == 1: return 1 else: return n * factorial(n  1) print(factorial(5)) # Outputs: 120
h. Dynamic Programming
1 2 3 4 5 6 7 8 9 10 11
def fib(n, dp): if n <= 1: return n if dp[n] != 1: return dp[n] dp[n] = fib(n  1, dp) + fib(n  2, dp) return dp[n] n = 10 dp = [1 for _ in range(n + 1)] print(fib(n, dp)) # Outputs: 55
Drills for Specific Needs of the Problem:
a. Checking if a substring is in a set
1 2 3
dictionary = {"apple", "orange", "banana"} s = "apple" print(s in dictionary) # Outputs: True
b. Minimum of two values
1 2 3
a = 10 b = 20 print(min(a, b)) # Outputs: 10
These drills are essential for our problem as checking if a substring is in a set is the main condition to decide whether to count extra characters or not. The minimum function is used to choose the smaller number of extra characters between two options.
Merging These Drills for the Final Solution:
The final solution is the combination of these drills. It starts by defining a function
func
that contains loops, conditionals, and recursion to check each substring ofs
. This function uses the dynamic programming concept to store the minimum number of extra characters for each prefix ofs
in the listdp
.In addition, the
minExtraChar
function is defined to initializedp
andst
, and then callfunc
for the entire strings
. This function integrates the use of list comprehension and set, along with the function definition and calling drills.The function
func
checks if each substrings[idx:j + 1]
is inst
and uses the minimum function to choose the smaller number of extra characters between two options. This involves the drills of substring, checking if a substring is in a set, and minimum of two values. In the end, the minimum number of extra characters for the entire strings
is returned, which is the value ofdp[0]
. This is the final goal of the problem.
Language Agnostic Coding Drills
Understanding Basic Programming Concepts: Variables, control flow (ifelse conditions), loops (forloop) and basic data structures like set, list, and dictionary.
Recursion: Recursive functions are those that call themselves during their execution. This is an important concept in many divideandconquer algorithms and data structures such as trees and graphs. In this code, the function
func
calls itself in the loop.Dynamic Programming: Dynamic Programming (DP) is a method for solving complex problems by breaking them down into simpler subproblems. It is applicable when the subproblems are not independent, and the solution to one subproblem may involve solutions to other subproblems. In the given code,
dp
array is used to store the results of subproblems.String Manipulation: Understanding how to manipulate and process strings is vital in many coding problems. This includes understanding string indexing, slicing and builtin string methods. In the given code, a substring of
s
is extracted in the linesubstr = s[idx:j + 1]
.Working with Infinity in Calculations: Infinity is often used in algorithms that deal with minimizing or maximizing values. It’s used here as an initial value for
res
, so that any other value will be smaller.ObjectOriented Programming: The problem is solved using a classbased approach, which is a key characteristic of objectoriented programming. The class
Solution
contains the methods to solve the problem.Set Data Structure: Sets are used for storing unique elements in an unordered collection. Here, the variable
st
is a set used to store thedictionary
.Function Arguments and Return Values: Understanding how to pass arguments to a function and how to return values from a function. This is shown throughout the code.
Problem Decomposition: The main problem is decomposed into subproblems which are solved individually, the results of which are used to solve the main problem. In this code, the main problem of finding the minimum extra characters is broken down into subproblems solved by the
func
method.
Targeted Drills in Python
Understanding Basic Programming Concepts:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# Drill 1 integer = 10 floating_point = 20.5 string = "Hello World" list_var = [1, 2, 3, 4, 5] set_var = {1, 2, 3, 4, 5} dictionary = {"name": "John", "age": 25} print(integer, floating_point, string, list_var, set_var, dictionary) # Drill 2 a, b, c = 10, 20, 30 if a >= b and a >= c: print(a) elif b >= a and b >= c: print(b) else: print(c) # Drill 3 n = 10 for i in range(1, n + 1): print(i)
Recursion:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# Drill 1 def factorial(n): if n == 0: return 1 else: return n * factorial(n  1) print(factorial(5)) # Drill 2 def fibonacci(n): if n <= 1: return n else: return fibonacci(n  1) + fibonacci(n  2) print(fibonacci(10))
Dynamic Programming:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# Drill 1 def fib(n): dp = [0, 1] + [0]*(n1) for i in range(2, n + 1): dp[i] = dp[i  1] + dp[i  2] return dp[n] print(fib(10)) # Drill 2 def lcs(X, Y): m, n = len(X), len(Y) dp = [[0]*(n+1) for _ in range(m+1)] for i in range(m+1): for j in range(n+1): if i == 0 or j == 0: dp[i][j] = 0 elif X[i1] == Y[j1]: dp[i][j] = dp[i1][j1] + 1 else: dp[i][j] = max(dp[i1][j], dp[i][j1]) return dp[m][n] print(lcs("ABCBDAB", "BDCAB"))
String Manipulation:
1 2 3 4 5 6 7 8 9
# Drill 1 def reverse_string(s): return s[::1] print(reverse_string("hello")) # Drill 2 def is_palindrome(s): return s == s[::1] print(is_palindrome("madam"))
Working with Infinity in Calculations:
1 2 3 4 5 6 7 8 9
# Drill 1 def min_and_max(numbers): min_val = float('inf') max_val = float('inf') for num in numbers: min_val = min(min_val, num) max_val = max(max_val, num) return min_val, max_val print(min_and_max([1, 2, 3, 4, 5]))
ObjectOriented Programming:
1 2 3
# Drill 1 class Person: def __init__(self, name, age
): self.name = name self.age = age def set_attributes(self, name, age): self.name = name self.age = age def get_attributes(self): return self.name, self.age
Drill 2
class Employee(Person): def init(self, name, age, job_title): super().init(name, age) self.job_title = job_title
7. **Set Data Structure**:
```python
# Drill 1
def find_unique(elements):
return set(elements)
print(find_unique([1, 2, 3, 2, 1]))
# Drill 2
def intersection(lst1, lst2):
return list(set(lst1) & set(lst2))
print(intersection([1, 2, 3], [2, 3, 4]))
Function Arguments and Return Values:
1 2 3 4 5 6 7 8 9
# Drill 1 def add_numbers(num1, num2): return num1 + num2 print(add_numbers(5, 10)) # Drill 2 def min_max(numbers): return min(numbers), max(numbers) print(min_max([1, 2, 3, 4, 5]))
Problem Decomposition:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
# Drill 1 def is_prime(n): if n <= 1: return False for i in range(2, n): if n % i == 0: return False return True def primes(n): primes = [] for i in range(2, n): if is_prime(i): primes.append(i) return primes print(primes(10)) # Drill 2 def find_min(nums): return min(nums) def swap(nums, i, j): nums[i], nums[j] = nums[j], nums[i] def sort(nums): for i in range(len(nums)): min_index = i + nums[i:].index(find_min(nums[i:])) swap(nums, i, min_index) return nums print(sort([3, 2, 1, 5, 4]))