The Indian Engineer

Problem 2707 Extra Characters in a String

Posted on 7 mins

Array Hash-Table String Dynamic-Programming Trie

Problem Statement

Link - Problem 2707

Question

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:

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:

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:

Solution

class Solution {
    int solve(string s,unordered_map<string, int>& um, unordered_map<string, int>& memo) {
        if (s.empty()) {
            return 0;
        }

        if(memo.find(s) != memo.end()){
            return memo[s];
        }

        int extra = INT_MAX;
        string tmp = "";

        for (int i = 0; i < s.size(); i++) {
            tmp += s[i];
            if (um.find(tmp) != um.end()) {
                extra = min(extra, solve(s.substr(i + 1),um,memo));
            }
        }

        extra = min(extra, 1 + solve(s.substr(1),um,memo));
        memo[s] = extra;

        return extra;
    }

public:
    int minExtraChar(string s, vector<string>& dict) {
        unordered_map<string, int> um;
        unordered_map<string, int> memo;

        for (auto& val : dict) {
            um[val]++;
        }
        return solve(s,um,memo);
    }
};

Complexity Analysis

| Algorithm                   | Time Complexity | Space Complexity |
| --------------------------- | --------------- | ---------------- |
| Memoized Depth-First Search | O( m 2^n )      | O(n)             |

Explanation

Intial Thoughts

To solve this problem, we need to break down the string into substrings that are present in the dictionary. We can start by checking each substring of the string to see if it is in the dictionary. If it is, we can recursively check the remaining part of the string. We should also keep track of the minimum number of extra characters we find. Some key points to consider are: checking all possible substrings, using recursion to solve the problem, keeping track of the minimum number of extra characters, using a dictionary to store the words for quick lookup, and avoiding duplicate work by storing the results of subproblems.

Intuitive Analysis

One way to intuitively solve this problem is to think of it like trying to cut a piece of string into smaller pieces that match a set of given templates. We start with the whole string and try to cut it into the largest possible piece that matches one of the templates. If we can’t find a match, we cut off the smallest possible piece and try again. We keep track of how many pieces we have to cut off that don’t match any template, and that’s our answer. Some key points to consider are: working from left to right, using the longest possible matching template, keeping track of the number of extra characters, using recursion to solve the problem, and avoiding duplicate work by storing the results of subproblems. We should also consider using a memoization technique to store the results of subproblems and avoid redundant work.

1. Intuition

2. Implementation


Complexity Analysis

Time Complexity:

Space Complexity:


Footnote

This question is rated as Medium difficulty.

Hints

Can we use Dynamic Programming here?

Define DP[i] as the min extra character if breaking up s[0:i] optimally.


Similar Questions:

Title URL Difficulty
Word Break https://leetcode.com/problems/word-break Medium