File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change
1
+ import java .util .ArrayList ;
2
+ import java .util .Arrays ;
3
+ import java .util .HashSet ;
4
+ import java .util .List ;
5
+ import java .util .Set ;
6
+ import java .util .stream .Collectors ;
7
+
8
+ import java .util .ArrayList ;
9
+ import java .util .Arrays ;
10
+ import java .util .List ;
11
+ import java .util .stream .Collectors ;
12
+
13
+ class Solution {
14
+ public boolean wordBreak (String s , List <String > wordDict ) {
15
+ return dfs (s , wordDict , new HashSet <>());
16
+ }
17
+
18
+ private boolean dfs (String s , List <String > wordDict , Set <String > dp ) {
19
+ // ์ข
๋ฃ ์กฐ๊ฑด: ๋ฌธ์์ด์ด ๋น์ด ์์ผ๋ฉด ์ฑ๊ณต
20
+ if (s .isEmpty ()) return true ;
21
+
22
+ // ์ค๋ณต ํ์ ๋ฐฉ์ง
23
+ if (dp .contains (s )) return false ;
24
+
25
+ for (String word : wordDict ) {
26
+ if (s .startsWith (word )) {
27
+ // ๋จ์ด๋ฅผ ์ ๊ฑฐํ๊ณ ์ฌ๊ท ํธ์ถ
28
+ if (dfs (s .substring (word .length ()), wordDict , dp )) {
29
+ return true ;
30
+ }
31
+ }
32
+ }
33
+
34
+ // ๋จ์ด๋ฅผ ์ ๊ฑฐํ์ง ์๊ณ ๋์ด๊ฐ๋ ๊ฒฝ์ฐ๋ ํ์
35
+ dp .add (s ); // ํ์์ด ์คํจํ ์ํ ์ ์ฅ
36
+ return false ;
37
+ }
38
+ }
39
+
40
+
41
+
You canโt perform that action at this time.
0 commit comments