File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ class Solution :
2+ def wordBreak (self , s : str , wordDict : list [str ]) -> bool :
3+ """
4+ ์ฃผ์ด์ง ๋ฌธ์์ด s๋ฅผ ๋จ์ด ์ฌ์ wordDict์ ๋จ์ด๋ค๋ก ๋ถํ ํ ์ ์๋์ง ์ฌ๋ถ๋ฅผ ํ๋จํ๋ ํจ์.
5+ N์ ๋ฌธ์์ด s์ ๊ธธ์ด, M์ ๋จ์ด ์ฌ์ ์ ์ต๋ ๋จ์ด ๊ธธ์ด๋ก ๊ฐ์ ํ ๋,
6+ ์๊ฐ ๋ณต์ก๋: O(N * M)
7+ ๊ณต๊ฐ ๋ณต์ก๋: O(N)
8+
9+ Args:
10+ s (str): ์ฃผ์ด์ง ๋ฌธ์์ด
11+ wordDict (list[str]): ์ฌ์ฉ๊ฐ๋ฅํ ๋จ์ด ์ฌ์
12+
13+ Returns:
14+ bool: ์ฃผ์ด์ง ๋ฌธ์์ด์ ๋จ์ด ์ฌ์ ์ ๋จ์ด๋ค๋ก ๋ถํ ํ ์ ์๋์ง ์ฌ๋ถ
15+ """
16+ word_check = [False ] * (len (s ) + 1 )
17+ max_len = len (max (wordDict , key = len ))
18+ word_check [0 ] = True
19+ for i in range (len (s )):
20+ if not word_check [i ]:
21+ continue
22+ for j in range (i + 1 , min (len (s ), i + max_len ) + 1 ):
23+ # i๊น์ง์ ๋จ์ด๊ฐ ์ฌ์ ์ ์๊ณ , i๋ถํฐ j๊น์ง์ ๋จ์ด๊ฐ ์ฌ์ ์ ์์ผ๋ฉด ๋ชจ๋ ๋จ์ด๊ฐ ์ฌ์ ์ ์๋ ์ํ๋ก ํ๋จ
24+ if word_check [i ] and s [i :j ] in wordDict :
25+ word_check [j ] = True
26+ return word_check [- 1 ]
You canโt perform that action at this time.
0 commit comments