Skip to content

Commit e624c7b

Browse files
donghyeon95donghyeon95
authored andcommitted
feat: Word Break #271
1 parent 6609876 commit e624c7b

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+

0 commit comments

Comments
ย (0)