File tree Expand file tree Collapse file tree 2 files changed +33
-0
lines changed Expand file tree Collapse file tree 2 files changed +33
-0
lines changed Original file line number Diff line number Diff line change
1
+ import java .util .*;
2
+
3
+ class Solution {
4
+ public boolean wordBreak (String s , List <String > wordDict ) {
5
+ Set <String > wordSet = new HashSet <>(wordDict );
6
+ boolean [] dp = new boolean [s .length () + 1 ];
7
+ dp [0 ] = true ;
8
+
9
+ for (int i = 1 ; i < s .length () + 1 ; i ++) {
10
+ for (int j = 0 ; j < i ; j ++) {
11
+ if (dp [j ] && (wordSet .contains (s .substring (j , i )))) {
12
+ dp [i ] = true ;
13
+ break ;
14
+ }
15
+ }
16
+ }
17
+ return dp [s .length ()];
18
+ }
19
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def wordBreak (self , s : str , wordDict : List [str ]) -> bool :
3
+ word_set = set (wordDict ) # O(1) ์กฐํ๋ฅผ ์ํด set์ผ๋ก ๋ณํ
4
+ n = len (s )
5
+ dp = [False ] * (n + 1 )
6
+ dp [0 ] = True # ๊ณต์งํฉ์ ํญ์ ๊ฐ๋ฅ
7
+
8
+ for i in range (1 , n + 1 ):
9
+ for j in range (i ):
10
+ if dp [j ] and s [j :i ] in word_set :
11
+ dp [i ] = True
12
+ break # i๋ฒ์งธ๊น์ง ๋๋ ์ ์์ผ๋ฉด ๋ ํ์ธํ ํ์ ์์
13
+
14
+ return dp [- 1 ]
You canโt perform that action at this time.
0 commit comments