Skip to content

Commit 9b87b32

Browse files
committed
feat: update solutions to lc/lcci problems
* lc No.0139.Word Break * lcci No.17.15.Longest Word
1 parent 522d92d commit 9b87b32

File tree

9 files changed

+48
-61
lines changed

9 files changed

+48
-61
lines changed

lcci/17.15.Longest Word/README.md

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,7 @@ class Solution:
6262
return -1 if a > b else 1
6363

6464
def dfs(w):
65-
if not w:
66-
return True
67-
for i in range(1, len(w) + 1):
68-
if trie.search(w[:i]) and dfs(w[i:]):
69-
return True
70-
return False
65+
return not w or any(trie.search(w[:i]) and dfs(w[i:]) for i in range(1, len(w) + 1))
7166

7267
words.sort(key=cmp_to_key(cmp))
7368
trie = Trie()

lcci/17.15.Longest Word/README_EN.md

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,7 @@ class Solution:
6464
return -1 if a > b else 1
6565

6666
def dfs(w):
67-
if not w:
68-
return True
69-
for i in range(1, len(w) + 1):
70-
if trie.search(w[:i]) and dfs(w[i:]):
71-
return True
72-
return False
67+
return not w or any(trie.search(w[:i]) and dfs(w[i:]) for i in range(1, len(w) + 1))
7368

7469
words.sort(key=cmp_to_key(cmp))
7570
trie = Trie()

lcci/17.15.Longest Word/Solution.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,7 @@ def cmp(a, b):
3030
return -1 if a > b else 1
3131

3232
def dfs(w):
33-
if not w:
34-
return True
35-
for i in range(1, len(w) + 1):
36-
if trie.search(w[:i]) and dfs(w[i:]):
37-
return True
38-
return False
33+
return not w or any(trie.search(w[:i]) and dfs(w[i:]) for i in range(1, len(w) + 1))
3934

4035
words.sort(key=cmp_to_key(cmp))
4136
trie = Trie()

solution/0100-0199/0139.Word Break/README.md

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,11 @@
5252

5353
<!-- 这里可写通用的实现逻辑 -->
5454

55-
动态规划法。
55+
**方法一:动态规划**
5656

57-
`dp[i]` 表示前 i 个字符组成的字符串 `s[0...i-1]` 能否拆分成若干个字典中出现的单词。
57+
$dp[i]$ 表示前 $i$ 个字符组成的字符串 $s[0...i-1]$ 能否拆分成若干个字典中出现的单词。
58+
59+
时间复杂度 $O(n^2)$。
5860

5961
<!-- tabs:start -->
6062

@@ -74,7 +76,7 @@ class Solution:
7476
if dp[j] and s[j:i] in words:
7577
dp[i] = True
7678
break
77-
return dp[n]
79+
return dp[-1]
7880
```
7981

8082
### **Java**
@@ -107,16 +109,16 @@ class Solution {
107109
class Solution {
108110
public:
109111
bool wordBreak(string s, vector<string>& wordDict) {
110-
unordered_set<string> words;
111-
for (auto word : wordDict) {
112-
words.insert(word);
113-
}
112+
unordered_set<string> words(wordDict.begin(), wordDict.end());
114113
int n = s.size();
115-
vector<bool> dp(n + 1, false);
114+
vector<bool> dp(n + 1);
116115
dp[0] = true;
117-
for (int i = 1; i <= n; ++i) {
118-
for (int j = 0; j < i; ++j) {
119-
if (dp[j] && words.find(s.substr(j, i - j)) != words.end()) {
116+
for (int i = 1; i <= n; ++i)
117+
{
118+
for (int j = 0; j < i; ++j)
119+
{
120+
if (dp[j] && words.count(s.substr(j, i - j)))
121+
{
120122
dp[i] = true;
121123
break;
122124
}

solution/0100-0199/0139.Word Break/README_EN.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class Solution:
6464
if dp[j] and s[j:i] in words:
6565
dp[i] = True
6666
break
67-
return dp[n]
67+
return dp[-1]
6868
```
6969

7070
### **Java**
@@ -95,16 +95,16 @@ class Solution {
9595
class Solution {
9696
public:
9797
bool wordBreak(string s, vector<string>& wordDict) {
98-
unordered_set<string> words;
99-
for (auto word : wordDict) {
100-
words.insert(word);
101-
}
98+
unordered_set<string> words(wordDict.begin(), wordDict.end());
10299
int n = s.size();
103-
vector<bool> dp(n + 1, false);
100+
vector<bool> dp(n + 1);
104101
dp[0] = true;
105-
for (int i = 1; i <= n; ++i) {
106-
for (int j = 0; j < i; ++j) {
107-
if (dp[j] && words.find(s.substr(j, i - j)) != words.end()) {
102+
for (int i = 1; i <= n; ++i)
103+
{
104+
for (int j = 0; j < i; ++j)
105+
{
106+
if (dp[j] && words.count(s.substr(j, i - j)))
107+
{
108108
dp[i] = true;
109109
break;
110110
}

solution/0100-0199/0139.Word Break/Solution.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
class Solution {
22
public:
33
bool wordBreak(string s, vector<string>& wordDict) {
4-
unordered_set<string> words;
5-
for (auto word : wordDict) {
6-
words.insert(word);
7-
}
4+
unordered_set<string> words(wordDict.begin(), wordDict.end());
85
int n = s.size();
9-
vector<bool> dp(n + 1, false);
6+
vector<bool> dp(n + 1);
107
dp[0] = true;
11-
for (int i = 1; i <= n; ++i) {
12-
for (int j = 0; j < i; ++j) {
13-
if (dp[j] && words.find(s.substr(j, i - j)) != words.end()) {
8+
for (int i = 1; i <= n; ++i)
9+
{
10+
for (int j = 0; j < i; ++j)
11+
{
12+
if (dp[j] && words.count(s.substr(j, i - j)))
13+
{
1414
dp[i] = true;
1515
break;
1616
}

solution/0100-0199/0139.Word Break/Solution.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@ public bool WordBreak(string s, IList<string> wordDict) {
44
int n = s.Length;
55
var dp = new bool[n + 1];
66
dp[0] = true;
7-
for (int i = 1; i <= n; ++i) {
8-
for (int j = 0; j < i; ++j) {
9-
if (dp[j] && words.Contains(s.Substring(j, i))) {
7+
for (int i = 1; i <= n; ++i)
8+
{
9+
for (int j = 0; j < i; ++j)
10+
{
11+
if (dp[j] && words.Contains(s.Substring(j, i - j)))
12+
{
1013
dp[i] = true;
1114
break;
1215
}

solution/0100-0199/0139.Word Break/Solution.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
public class Solution {
2-
public bool WordBreak(string s, IList<string> wordDict) {
3-
var words = new HashSet<string>(wordDict);
4-
int n = s.Length;
5-
var dp = new bool[n + 1];
1+
class Solution {
2+
public boolean wordBreak(String s, List<String> wordDict) {
3+
Set<String> words = new HashSet<>(wordDict);
4+
int n = s.length();
5+
boolean[] dp = new boolean[n + 1];
66
dp[0] = true;
7-
for (int i = 1; i <= n; ++i)
8-
{
9-
for (int j = 0; j < i; ++j)
10-
{
11-
if (dp[j] && words.Contains(s.Substring(j, i - j)))
12-
{
7+
for (int i = 1; i <= n; ++i) {
8+
for (int j = 0; j < i; ++j) {
9+
if (dp[j] && words.contains(s.substring(j, i))) {
1310
dp[i] = true;
1411
break;
1512
}

solution/0100-0199/0139.Word Break/Solution.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ def wordBreak(self, s: str, wordDict: List[str]) -> bool:
99
if dp[j] and s[j:i] in words:
1010
dp[i] = True
1111
break
12-
return dp[n]
12+
return dp[-1]

0 commit comments

Comments
 (0)