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