Skip to content

Commit a35c403

Browse files
committed
word-break solutions
1 parent d04660f commit a35c403

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

โ€Žword-break/Blossssom.tsโ€Ž

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* @param s - ํƒ€๊ฒŸ ๋ฌธ์ž์—ด
3+
* @param wordDict - ๋ฌธ์ž์—ด ๋ฐฐ์—ด
4+
* @returns - wordDict์˜ ์š”์†Œ๋ฅผ ์กฐํ•ฉํ•ด ํƒ€๊ฒŸ ๋ฌธ์ž์—ด์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š”์ง€ return
5+
* @description
6+
* - wordDict์˜ ์š”์†Œ๋Š” ์ค‘๋ณต ์‚ฌ์šฉ ๊ฐ€๋Šฅ
7+
* - dp ๊นŒ์ง„ ์ƒ๊ฐํ–ˆ์ง€๋งŒ ์„ธ๋ถ€ ๊ตฌํ˜„๊นŒ์ง„ ํž˜๋“ค์—ˆ์Œ
8+
*/
9+
10+
function wordBreak(s: string, wordDict: string[]): boolean {
11+
// ๊ฐ ์š”์†Œ๋ณ„ ๋‚จ์€ ๋‹จ์–ด๋ฅผ ์ €์žฅํ•œ๋‹ค๋ฉด?
12+
const dictSet = new Set(wordDict);
13+
const dp = Array.from({ length: s.length + 1 }, () => false);
14+
15+
dp[0] = true;
16+
17+
for (let i = 1; i <= s.length; i++) {
18+
for (let j = 0; j < i; j++) {
19+
if (dp[j] && dictSet.has(s.slice(j, i))) {
20+
dp[i] = true;
21+
break;
22+
}
23+
}
24+
}
25+
26+
return dp[s.length];
27+
}
28+
29+
const s = "catsandog";
30+
const wordDict = ["cats", "dog", "sand", "and", "cat"];
31+
32+
const answer = wordBreak(s, wordDict);
33+
console.log("is answer :", answer);
34+
35+

0 commit comments

Comments
ย (0)