We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent bfa6b5b commit d759e3cCopy full SHA for d759e3c
2707-extra-characters-in-a-string/2707-extra-characters-in-a-string.cpp
@@ -0,0 +1,26 @@
1
+class Solution {
2
+public:
3
+ map<string, bool> mp;
4
+ vector<int> dp;
5
+ int minExtraChar(string s, vector<string>& dictionary) {
6
+ int n = s.size();
7
+ for (auto& i : dictionary) {
8
+ mp[i] = true;
9
+ }
10
+ dp.assign(n + 5, -1);
11
+ return solve(0, s);
12
13
+ int solve(int i, string& str) {
14
+ if (i == str.size()) return 0;
15
+ if (dp[i] != -1) return dp[i];
16
+ int ans = 1 + solve(i + 1, str);
17
+ string temp;
18
+ for (int j = i; j < str.size(); j++) {
19
+ temp += str[j];
20
+ if (mp[temp]) {
21
+ ans = min(ans, solve(j + 1, str));
22
23
24
+ return dp[i] = ans;
25
26
+};
0 commit comments