Skip to content

Commit 3fda942

Browse files
committed
feat: add solutions to lcci problem: No.17.13
No.17.13.Re-Space
1 parent 30b657b commit 3fda942

File tree

6 files changed

+225
-11
lines changed

6 files changed

+225
-11
lines changed

lcci/17.13.Re-Space/README.md

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,103 @@ sentence = "jesslookedjustliketimherbrother"
3232

3333
<!-- 这里可写通用的实现逻辑 -->
3434

35+
**方法一:动态规划**
36+
3537
<!-- tabs:start -->
3638

3739
### **Python3**
3840

3941
<!-- 这里可写当前语言的特殊实现逻辑 -->
4042

4143
```python
42-
44+
class Solution:
45+
def respace(self, dictionary: List[str], sentence: str) -> int:
46+
s = set(dictionary)
47+
n = len(sentence)
48+
dp = [0] * (n + 1)
49+
for i in range(1, n + 1):
50+
dp[i] = dp[i - 1] + 1
51+
for j in range(i):
52+
if sentence[j: i] in s:
53+
dp[i] = min(dp[i], dp[j])
54+
return dp[-1]
4355
```
4456

4557
### **Java**
4658

4759
<!-- 这里可写当前语言的特殊实现逻辑 -->
4860

4961
```java
62+
class Solution {
63+
public int respace(String[] dictionary, String sentence) {
64+
Set<String> dict = new HashSet<>(Arrays.asList(dictionary));
65+
int n = sentence.length();
66+
int[] dp = new int[n + 1];
67+
for (int i = 1; i <= n; i++) {
68+
dp[i] = dp[i - 1] + 1;
69+
for (int j = 0; j < i; ++j) {
70+
if (dict.contains(sentence.substring(j, i))) {
71+
dp[i] = Math.min(dp[i], dp[j]);
72+
}
73+
}
74+
}
75+
return dp[n];
76+
}
77+
}
78+
```
79+
80+
### **C++**
81+
82+
```cpp
83+
class Solution {
84+
public:
85+
int respace(vector<string>& dictionary, string sentence) {
86+
unordered_set<string> s(dictionary.begin(), dictionary.end());
87+
int n = sentence.size();
88+
vector<int> dp(n + 1);
89+
for (int i = 1; i <= n; ++i)
90+
{
91+
dp[i] = dp[i - 1] + 1;
92+
for (int j = 0; j < i; ++j)
93+
{
94+
if (s.count(sentence.substr(j, i - j)))
95+
{
96+
dp[i] = min(dp[i], dp[j]);
97+
}
98+
}
99+
}
100+
return dp[n];
101+
}
102+
};
103+
```
50104
105+
### **Go**
106+
107+
```go
108+
func respace(dictionary []string, sentence string) int {
109+
s := map[string]bool{}
110+
for _, v := range dictionary {
111+
s[v] = true
112+
}
113+
n := len(sentence)
114+
dp := make([]int, n+1)
115+
for i := 1; i <= n; i++ {
116+
dp[i] = dp[i-1] + 1
117+
for j := 0; j < i; j++ {
118+
if s[sentence[j:i]] {
119+
dp[i] = min(dp[i], dp[j])
120+
}
121+
}
122+
}
123+
return dp[n]
124+
}
125+
126+
func min(a, b int) int {
127+
if a < b {
128+
return a
129+
}
130+
return b
131+
}
51132
```
52133

53134
### **...**

lcci/17.13.Re-Space/README_EN.md

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,92 @@ sentence = &quot;jesslookedjustliketimherbrother&quot;
4141
### **Python3**
4242

4343
```python
44-
44+
class Solution:
45+
def respace(self, dictionary: List[str], sentence: str) -> int:
46+
s = set(dictionary)
47+
n = len(sentence)
48+
dp = [0] * (n + 1)
49+
for i in range(1, n + 1):
50+
dp[i] = dp[i - 1] + 1
51+
for j in range(i):
52+
if sentence[j: i] in s:
53+
dp[i] = min(dp[i], dp[j])
54+
return dp[-1]
4555
```
4656

4757
### **Java**
4858

4959
```java
60+
class Solution {
61+
public int respace(String[] dictionary, String sentence) {
62+
Set<String> dict = new HashSet<>(Arrays.asList(dictionary));
63+
int n = sentence.length();
64+
int[] dp = new int[n + 1];
65+
for (int i = 1; i <= n; i++) {
66+
dp[i] = dp[i - 1] + 1;
67+
for (int j = 0; j < i; ++j) {
68+
if (dict.contains(sentence.substring(j, i))) {
69+
dp[i] = Math.min(dp[i], dp[j]);
70+
}
71+
}
72+
}
73+
return dp[n];
74+
}
75+
}
76+
```
77+
78+
### **C++**
79+
80+
```cpp
81+
class Solution {
82+
public:
83+
int respace(vector<string>& dictionary, string sentence) {
84+
unordered_set<string> s(dictionary.begin(), dictionary.end());
85+
int n = sentence.size();
86+
vector<int> dp(n + 1);
87+
for (int i = 1; i <= n; ++i)
88+
{
89+
dp[i] = dp[i - 1] + 1;
90+
for (int j = 0; j < i; ++j)
91+
{
92+
if (s.count(sentence.substr(j, i - j)))
93+
{
94+
dp[i] = min(dp[i], dp[j]);
95+
}
96+
}
97+
}
98+
return dp[n];
99+
}
100+
};
101+
```
50102
103+
### **Go**
104+
105+
```go
106+
func respace(dictionary []string, sentence string) int {
107+
s := map[string]bool{}
108+
for _, v := range dictionary {
109+
s[v] = true
110+
}
111+
n := len(sentence)
112+
dp := make([]int, n+1)
113+
for i := 1; i <= n; i++ {
114+
dp[i] = dp[i-1] + 1
115+
for j := 0; j < i; j++ {
116+
if s[sentence[j:i]] {
117+
dp[i] = min(dp[i], dp[j])
118+
}
119+
}
120+
}
121+
return dp[n]
122+
}
123+
124+
func min(a, b int) int {
125+
if a < b {
126+
return a
127+
}
128+
return b
129+
}
51130
```
52131

53132
### **...**

lcci/17.13.Re-Space/Solution.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
int respace(vector<string>& dictionary, string sentence) {
4+
unordered_set<string> s(dictionary.begin(), dictionary.end());
5+
int n = sentence.size();
6+
vector<int> dp(n + 1);
7+
for (int i = 1; i <= n; ++i)
8+
{
9+
dp[i] = dp[i - 1] + 1;
10+
for (int j = 0; j < i; ++j)
11+
{
12+
if (s.count(sentence.substr(j, i - j)))
13+
{
14+
dp[i] = min(dp[i], dp[j]);
15+
}
16+
}
17+
}
18+
return dp[n];
19+
}
20+
};

lcci/17.13.Re-Space/Solution.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
func respace(dictionary []string, sentence string) int {
2+
s := map[string]bool{}
3+
for _, v := range dictionary {
4+
s[v] = true
5+
}
6+
n := len(sentence)
7+
dp := make([]int, n+1)
8+
for i := 1; i <= n; i++ {
9+
dp[i] = dp[i-1] + 1
10+
for j := 0; j < i; j++ {
11+
if s[sentence[j:i]] {
12+
dp[i] = min(dp[i], dp[j])
13+
}
14+
}
15+
}
16+
return dp[n]
17+
}
18+
19+
func min(a, b int) int {
20+
if a < b {
21+
return a
22+
}
23+
return b
24+
}

lcci/17.13.Re-Space/Solution.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
class Solution {
22
public int respace(String[] dictionary, String sentence) {
3-
Set<String> set = new HashSet<>(dictionary.length);
4-
set.addAll(Arrays.asList(dictionary));
5-
6-
int[] dp = new int[sentence.length() + 1];
7-
for (int i = 1; i <= sentence.length(); i++) {
3+
Set<String> dict = new HashSet<>(Arrays.asList(dictionary));
4+
int n = sentence.length();
5+
int[] dp = new int[n + 1];
6+
for (int i = 1; i <= n; i++) {
87
dp[i] = dp[i - 1] + 1;
9-
for (int j = 0;j < i;j++) {
10-
if (set.contains(sentence.substring(j, i))) {
11-
dp[i] = Math.min(dp[i], dp[j]);
8+
for (int j = 0; j < i; ++j) {
9+
if (dict.contains(sentence.substring(j, i))) {
10+
dp[i] = Math.min(dp[i], dp[j]);
1211
}
1312
}
1413
}
15-
return dp[sentence.length()];
14+
return dp[n];
1615
}
1716
}

lcci/17.13.Re-Space/Solution.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def respace(self, dictionary: List[str], sentence: str) -> int:
3+
s = set(dictionary)
4+
n = len(sentence)
5+
dp = [0] * (n + 1)
6+
for i in range(1, n + 1):
7+
dp[i] = dp[i - 1] + 1
8+
for j in range(i):
9+
if sentence[j: i] in s:
10+
dp[i] = min(dp[i], dp[j])
11+
return dp[-1]

0 commit comments

Comments
 (0)