Skip to content

Commit 03fad27

Browse files
committed
265th Commit
1 parent c7ed89a commit 03fad27

File tree

4 files changed

+49
-2
lines changed

4 files changed

+49
-2
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -581,12 +581,13 @@ Problems:
581581
| ----------------------------------- | --------------- | ------ | ---- |
582582
| 70. Climbing Stairs | [Solution][70] | Easy | 詳解 |
583583
| 198. House Robber | [Solution][198] | Medium | 詳解 |
584-
| 139. Word Break | Solution | Medium | 詳解 |
584+
| 139. Word Break | [Solution][139] | Medium | 詳解 |
585585
| 322. Coin Change | Solution | Medium | 詳解 |
586586
| 300. Longest Increasing Subsequence | Solution | Medium | 詳解 |
587587

588588
[70]: ./src/page-1/70.%20Climbing%20Stairs/climbStairs.ts
589589
[198]: ./src/page-2/198.%20House%20Robber/rob.ts
590+
[139]: ./src/page-2/139.%20Word%20Break/wordBreak.ts
590591

591592
| Multidimensional DP | | | |
592593
| ---------------------------------------- | -------------- | ------ | ---------------------- |
@@ -676,7 +677,7 @@ Problems:
676677
| 70. Climbing Stairs | [Solution][70] | Easy | 詳解 |
677678
| 72. Edit Distance | [Solution][72] | Medium | [詳解][72-explanation] |
678679
| 118. Pascal's Triangle | [Solution][118] | Easy | 詳解 |
679-
| 139. Word Break | Solution | Medium | 詳解 |
680+
| 139. Word Break | [Solution][139] | Medium | 詳解 |
680681
| 152. Maximum Product Subarray | Solution | Medium | 詳解 |
681682
| 198. House Robber | [Solution][198] | Medium | 詳解 |
682683
| 279. Perfect Squares | Solution | Medium | 詳解 |
@@ -691,6 +692,7 @@ Problems:
691692
[72]: ./src/page-1/72.%20Edit%20Distance/minDistance.ts
692693
[72-explanation]: ./src/page-1/72.%20Edit%20Distance/README.md
693694
[118]: ./src/page-2/118.%20Pascal's%20Triangle/generate.ts
695+
[139]: ./src/page-2/139.%20Word%20Break/wordBreak.ts
694696
[198]: ./src/page-2/198.%20House%20Robber/rob.ts
695697
[1143]: ./src/page-11/1143.%20Longest%20Common%20Subsequence/longestCommonSubsequence.ts
696698

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { wordBreak } from './wordBreak';
2+
3+
describe('139. Word Break', () => {
4+
test('wordBreak', () => {
5+
{
6+
const s = 'leetcode';
7+
const wordDict = ['leet', 'code'];
8+
expect(wordBreak(s, wordDict)).toBe(true);
9+
}
10+
11+
{
12+
const s = 'applepenapple';
13+
const wordDict = ['apple', 'pen'];
14+
expect(wordBreak(s, wordDict)).toBe(true);
15+
}
16+
17+
{
18+
const s = 'catsandog';
19+
const wordDict = ['cats', 'dog', 'sand', 'and', 'cat'];
20+
expect(wordBreak(s, wordDict)).toBe(false);
21+
}
22+
});
23+
});
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
type WordBreak = (s: string, wordDict: string[]) => boolean;
2+
3+
/**
4+
* Accepted
5+
*/
6+
export const wordBreak: WordBreak = (s, wordDict) => {
7+
const wordSet = new Set(wordDict);
8+
const dp: boolean[] = Array(s.length + 1).fill(false);
9+
10+
dp[0] = true;
11+
12+
for (let i = 1; i <= s.length; i++) {
13+
for (let j = 0; j < i; j++) {
14+
if (dp[j] && wordSet.has(s.substring(j, i))) {
15+
dp[i] = true;
16+
break;
17+
}
18+
}
19+
}
20+
21+
return dp[s.length];
22+
};
File renamed without changes.

0 commit comments

Comments
 (0)