File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * ์ฃผ์ด์ง ๋ฌธ์์ด์ด ๋จ์ด ์ฌ์ ์ ์๋ ๋จ์ด๋ค๋ก ๋๋์ด์ง ์ ์๋์ง ํ์ธํฉ๋๋ค.
3
+ *
4
+ *
5
+ * @param {string } s - ํ์ธํ ๋ฌธ์์ด
6
+ * @param {string[] } wordDict - ๋จ์ด ์ฌ์
7
+ * @returns {boolean } ๋ฌธ์์ด์ด ๋จ์ด๋ก ์๋ฒฝํ ๋๋์ด์ง ์ ์์ผ๋ฉด `true`, ์๋๋ฉด `false`
8
+ *
9
+ * ์๊ฐ ๋ณต์ก์ฑ O(n * m * k)
10
+ * - n: ๋ฌธ์์ด s ๊ธธ์ด
11
+ * - m: ๋จ์ด ์ฌ์ ๊ธธ์ด
12
+ * - k: ๋จ์ด ์ฌ์ ๋ด ๋จ์ด ๊ธธ์ด
13
+ *
14
+ * ๊ณต๊ฐ ๋ณต์ ์ฑ O(n)
15
+ * - ๋ฉ๋ชจ์ด์ ์ด์
(memo) ๋ฐ ์ฌ๊ท ํธ์ถ ์คํ ํฌ๊ธฐ๊ฐ ๋ฌธ์์ด ๊ธธ์ด n์ ๋น๋ก.
16
+ */
17
+ function wordBreak ( s : string , wordDict : string [ ] ) : boolean {
18
+ const memo : Record < number , boolean > = { } ;
19
+
20
+ /**
21
+ * @param {number } start - ํ์ฌ ์์ idx
22
+ * @returns {boolean } ์ฃผ์ด์ง idx ๋ถํฐ ๋ฌธ์์ด์ ๋๋ ์ ์์ผ๋ฉด true
23
+ */
24
+ const dfs = ( start : number ) : boolean => {
25
+ if ( start in memo ) return memo [ start ] ;
26
+ if ( start === s . length ) return ( memo [ start ] = true ) ;
27
+
28
+ for ( const word of wordDict ) {
29
+ if ( s . startsWith ( word , start ) && dfs ( start + word . length ) ) {
30
+ return ( memo [ start ] = true ) ;
31
+ }
32
+ }
33
+
34
+ return ( memo [ start ] = false ) ;
35
+ } ;
36
+
37
+ return dfs ( 0 ) ;
38
+ }
39
+
You canโt perform that action at this time.
0 commit comments