Skip to content

Commit d759e3c

Browse files
committed
Time: 602 ms (6.42%), Space: 202.1 MB (6.59%) - LeetHub
1 parent bfa6b5b commit d759e3c

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)