File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed
Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change 1+ // wordDict 조합으로 s를 만들 수 있는지 반환해라
2+ // TC: O(n^2) SC: O(n + m * k) => s의 길이 + (단어 수 * 평균 단어 길이)
3+ // .has(word) => O(1)
4+
5+ function wordBreak ( s : string , wordDict : string [ ] ) : boolean {
6+ const wordSet = new Set ( wordDict ) ;
7+ const dp : boolean [ ] = Array ( s . length + 1 ) . fill ( false ) ;
8+ dp [ 0 ] = true ;
9+
10+ for ( let i = 1 ; i <= s . length ; i ++ ) {
11+ for ( let j = 0 ; j < i ; j ++ ) {
12+ if ( dp [ j ] && wordSet . has ( s . slice ( j , i ) ) ) {
13+ dp [ i ] = true ;
14+ break ;
15+ }
16+ }
17+ }
18+ return dp [ s . length ] ;
19+ }
20+
21+ // PY 풀이
22+ // class Solution:
23+ // def wordBreak(self, s: str, wordDict: List[str]) -> bool:
24+ // wordSet = set(wordDict)
25+ // dp = [False] * (len(s)+1)
26+ // dp[0] = True
27+
28+ // for i in range(1, len(s)+1):
29+ // for j in range(0, i):
30+ // if dp[j] and s[j:i] in wordSet:
31+ // dp[i] = True
32+
33+ // return dp[len(s)]
You can’t perform that action at this time.
0 commit comments