Skip to content

Commit 73b72fd

Browse files
committed
test: 2707 solution
py, go, c++
1 parent 430d94a commit 73b72fd

File tree

10 files changed

+189
-4
lines changed

10 files changed

+189
-4
lines changed

MODULE.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ new_local_repository = use_repo_rule("@bazel_tools//tools/build_defs/repo:local.
1717
new_local_repository(
1818
name = "problems",
1919
build_file = "//cpp:solution.BUILD",
20-
path = "problems/problems_386/",
20+
path = "problems/problems_2707/",
2121
)
2222

2323
new_local_repository(

golang/solution_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package golang
22

33
import (
4-
problem "leetCode/problems/problems_386"
4+
problem "leetCode/problems/problems_2707"
55
"testing"
66
)
77

88
func TestSolution(t *testing.T) {
9-
TestEach(t, "386", "problems", problem.Solve)
9+
TestEach(t, "2707", "problems", problem.Solve)
1010
}

problems/problems_2707/Solution.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//go:build ignore
2+
#include "cpp/common/Solution.h"
3+
4+
#include <unordered_set>
5+
#include <vector>
6+
7+
using namespace std;
8+
using json = nlohmann::json;
9+
10+
class Solution {
11+
public:
12+
int minExtraChar(string s, vector<string>& dictionary) {
13+
unordered_set<string> cache(dictionary.begin(), dictionary.end());
14+
int n = s.size();
15+
vector<int> dp(n + 1, 0);
16+
for (int i = 0; i < n; ++i) {
17+
dp[i + 1] = dp[i] + 1; // Assume the worst case: every character is an extra character
18+
for (int j = 0; j <= i; ++j) {
19+
string sub = s.substr(j, i - j + 1);
20+
if (cache.find(sub) != cache.end()) {
21+
dp[i + 1] = min(dp[i + 1], dp[j]); // If the substring is in the dictionary, no extra character needed
22+
}
23+
}
24+
}
25+
return dp[n];
26+
}
27+
};
28+
29+
json leetcode::qubh::Solve(string input_json_values) {
30+
vector<string> inputArray;
31+
size_t pos = input_json_values.find('\n');
32+
while (pos != string::npos) {
33+
inputArray.push_back(input_json_values.substr(0, pos));
34+
input_json_values = input_json_values.substr(pos + 1);
35+
pos = input_json_values.find('\n');
36+
}
37+
inputArray.push_back(input_json_values);
38+
39+
Solution solution;
40+
string s = json::parse(inputArray.at(0));
41+
vector<string> dictionary = json::parse(inputArray.at(1));
42+
return solution.minExtraChar(s, dictionary);
43+
}

problems/problems_2707/problem.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# 2707. Extra Characters in a String [Rating: 1735.85]
2+
3+
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters</strong> in <code>s</code> which are not present in any of the substrings.</p>
4+
5+
<p>Return <em>the <strong>minimum</strong> number of extra characters left over if you break up </em><code>s</code><em> optimally.</em></p>
6+
7+
<p>&nbsp;</p>
8+
<p><strong class="example">Example 1:</strong></p>
9+
10+
<pre>
11+
<strong>Input:</strong> s = &quot;leetscode&quot;, dictionary = [&quot;leet&quot;,&quot;code&quot;,&quot;leetcode&quot;]
12+
<strong>Output:</strong> 1
13+
<strong>Explanation:</strong> We can break s in two substrings: &quot;leet&quot; from index 0 to 3 and &quot;code&quot; from index 5 to 8. There is only 1 unused character (at index 4), so we return 1.
14+
15+
</pre>
16+
17+
<p><strong class="example">Example 2:</strong></p>
18+
19+
<pre>
20+
<strong>Input:</strong> s = &quot;sayhelloworld&quot;, dictionary = [&quot;hello&quot;,&quot;world&quot;]
21+
<strong>Output:</strong> 3
22+
<strong>Explanation:</strong> We can break s in two substrings: &quot;hello&quot; from index 3 to 7 and &quot;world&quot; 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.
23+
</pre>
24+
25+
<p>&nbsp;</p>
26+
<p><strong>Constraints:</strong></p>
27+
28+
<ul>
29+
<li><code>1 &lt;= s.length &lt;= 50</code></li>
30+
<li><code>1 &lt;= dictionary.length &lt;= 50</code></li>
31+
<li><code>1 &lt;= dictionary[i].length &lt;= 50</code></li>
32+
<li><code>dictionary[i]</code>&nbsp;and <code>s</code> consists of only lowercase English letters</li>
33+
<li><code>dictionary</code> contains distinct words</li>
34+
</ul>

problems/problems_2707/problem_zh.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# 2707. 字符串中的额外字符 [难度分: 1735.85]
2+
3+
<p>给你一个下标从 <strong>0</strong>&nbsp;开始的字符串&nbsp;<code>s</code>&nbsp;和一个单词字典&nbsp;<code>dictionary</code>&nbsp;。你需要将&nbsp;<code>s</code>&nbsp;分割成若干个 <strong>互不重叠</strong>&nbsp;的子字符串,每个子字符串都在&nbsp;<code>dictionary</code>&nbsp;中出现过。<code>s</code>&nbsp;中可能会有一些&nbsp;<strong>额外的字符</strong>&nbsp;不在任何子字符串中。</p>
4+
5+
<p>请你采取最优策略分割 <code>s</code>&nbsp;,使剩下的字符 <strong>最少</strong>&nbsp;。</p>
6+
7+
<p>&nbsp;</p>
8+
9+
<p><strong>示例 1:</strong></p>
10+
11+
<pre><b>输入:</b>s = "leetscode", dictionary = ["leet","code","leetcode"]
12+
<b>输出:</b>1
13+
<b>解释:</b>将 s 分成两个子字符串:下标从 0 到 3 的 "leet" 和下标从 5 到 8 的 "code" 。只有 1 个字符没有使用(下标为 4),所以我们返回 1 。
14+
</pre>
15+
16+
<p><strong>示例 2:</strong></p>
17+
18+
<pre><b>输入:</b>s = "sayhelloworld", dictionary = ["hello","world"]
19+
<b>输出:</b>3
20+
<b>解释:</b>将 s 分成两个子字符串:下标从 3 到 7 的 "hello" 和下标从 8 到 12 的 "world" 。下标为 0 ,1 和 2 的字符没有使用,所以我们返回 3 。
21+
</pre>
22+
23+
<p>&nbsp;</p>
24+
25+
<p><strong>提示:</strong></p>
26+
27+
<ul>
28+
<li><code>1 &lt;= s.length &lt;= 50</code></li>
29+
<li><code>1 &lt;= dictionary.length &lt;= 50</code></li>
30+
<li><code>1 &lt;= dictionary[i].length &lt;= 50</code></li>
31+
<li><code>dictionary[i]</code>&nbsp;和&nbsp;<code>s</code>&nbsp;只包含小写英文字母。</li>
32+
<li><code>dictionary</code>&nbsp;中的单词互不相同。</li>
33+
</ul>

problems/problems_2707/solution.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package problem2707
2+
3+
import (
4+
"encoding/json"
5+
"log"
6+
"strings"
7+
)
8+
9+
func minExtraChar(s string, dictionary []string) int {
10+
d := make(map[string]bool, len(dictionary))
11+
for _, word := range dictionary {
12+
d[word] = true
13+
}
14+
n := len(s)
15+
dp := make([]int, n+1)
16+
for i := 0; i < n; i++ {
17+
dp[i+1] = dp[i] + 1 // assume the next character is an extra character
18+
for j := 0; j <= i; j++ {
19+
if d[s[j:i+1]] {
20+
// if the substring s[j:i+1] is in the dictionary, then we can remove it
21+
dp[i+1] = min(dp[i+1], dp[j])
22+
}
23+
}
24+
}
25+
return dp[n]
26+
}
27+
28+
func Solve(inputJsonValues string) any {
29+
inputValues := strings.Split(inputJsonValues, "\n")
30+
var s string
31+
var dictionary []string
32+
33+
if err := json.Unmarshal([]byte(inputValues[0]), &s); err != nil {
34+
log.Fatal(err)
35+
}
36+
if err := json.Unmarshal([]byte(inputValues[1]), &dictionary); err != nil {
37+
log.Fatal(err)
38+
}
39+
40+
return minExtraChar(s, dictionary)
41+
}

problems/problems_2707/solution.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import solution
2+
from typing import *
3+
4+
5+
class Solution(solution.Solution):
6+
def solve(self, test_input=None):
7+
return self.minExtraChar(*test_input)
8+
9+
def minExtraChar(self, s: str, dictionary: List[str]) -> int:
10+
dictionary = set(dictionary)
11+
n = len(s)
12+
dp = [0] * (n + 1)
13+
for i in range(1, n + 1):
14+
dp[i] = dp[i - 1] + 1
15+
for j in range(i):
16+
if s[j:i] in dictionary:
17+
dp[i] = min(dp[i], dp[j])
18+
return dp[n]

problems/problems_2707/testcase

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
["\"leetscode\"\n[\"leet\",\"code\",\"leetcode\"]", "\"sayhelloworld\"\n[\"hello\",\"world\"]"]
2+
[1, 3]

problems/problems_2707/testcase.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from collections import namedtuple
2+
import testcase
3+
4+
case = namedtuple("Testcase", ["Input", "Output"])
5+
6+
7+
class Testcase(testcase.Testcase):
8+
def __init__(self):
9+
self.testcases = []
10+
self.testcases.append(case(Input=['leetscode', ['leet', 'code', 'leetcode']], Output=1))
11+
self.testcases.append(case(Input=['sayhelloworld', ['hello', 'world']], Output=3))
12+
13+
def get_testcases(self):
14+
return self.testcases

python/test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
logging.basicConfig(level=logging.INFO, format=constants.LOGGING_FORMAT, datefmt=constants.DATE_FORMAT)
1212

1313
# Question ID that wants to test, modify here as passing arguments
14-
QUESTION = "3579"
14+
QUESTION = "2707"
1515
# QUESTION = "Interview/10_02"
1616
# QUESTION = "LCP/07"
1717
# QUESTION = "剑指Offer/52"

0 commit comments

Comments
 (0)