File tree Expand file tree Collapse file tree 1 file changed +55
-0
lines changed Expand file tree Collapse file tree 1 file changed +55
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ [๋ฌธ์ ํ์ด]
3
+ - startsWith์ผ๋ก ์์ผ๋ฉด substringํ๋ฉด์ ๋ฐ๋ณตํด๋ณด์.
4
+ - ์ฃผ์ด์ง list๋ฅผ map์ผ๋ก ํ์ฉ
5
+ - failed
6
+ s = "cars"
7
+ wordDict = ["car","ca","rs"]
8
+ ---
9
+ class Solution {
10
+ public boolean wordBreak(String s, List<String> wordDict) {
11
+ for (String word: wordDict) {
12
+ s = s.replace(word, "");
13
+ }
14
+
15
+ if (s.length() == 0) {
16
+ return true;
17
+ }
18
+ return false;
19
+ }
20
+ }
21
+
22
+ - DP
23
+ time: O(N^2), space: O(N)
24
+
25
+ [ํ๊ณ ]
26
+ ์ฒ์์ ๋๋ฌด ์ฝ๊ฒ ์๊ฐํด์ ์๋ ๊ฒ ๊ฐ๊ธดํ๋ค..
27
+ DP๋ก ํ๋ฉด ๋ ๊ฒ ๊ฐ์๋ฐ..
28
+ ํด์ค์ ๋ณด๋ฉด ์ดํด๊ฐ ๋์ง๋ง, ํญ์ DP ์ ๊ทผ์ด ์ ์๋๋ค..
29
+ */
30
+ class Solution {
31
+ public boolean wordBreak (String s , List <String > wordDict ) {
32
+ // "applepenapple", ["apple","pen"]
33
+ int sLen = s .length ();
34
+ boolean [] dp = new boolean [sLen + 1 ];
35
+ dp [0 ] = true ;
36
+
37
+ int maxLen = 0 ;
38
+ for (String word : wordDict ) {
39
+ maxLen = Math .max (maxLen , word .length ());
40
+ } // 5
41
+
42
+ for (int i = 1 ; i <= sLen ; i ++) { // 1 ~ 13
43
+ // applepenapple > apple > 5
44
+ for (int j = i - 1 ; j >= 0 ; j --) {
45
+ // applepenapple > apple > index:4 > 0
46
+ if (dp [j ] && wordDict .contains (s .substring (j , i ))) {
47
+ dp [i ] = true ;
48
+ break ;
49
+ }
50
+ }
51
+ }
52
+ return dp [sLen ];
53
+ }
54
+ }
55
+
You canโt perform that action at this time.
0 commit comments