|
| 1 | +package leetcode_study |
| 2 | + |
| 3 | +import io.kotest.matchers.shouldBe |
| 4 | +import org.junit.jupiter.api.Test |
| 5 | + |
| 6 | +class `word-break` { |
| 7 | + |
| 8 | + fun wordBreak(s: String, wordDict: List<String>): Boolean { |
| 9 | + return usingDP(s, wordDict) |
| 10 | + } |
| 11 | + |
| 12 | + /** |
| 13 | + * 1. DFS ์ฌ์ฉ (์๊ฐ์ด๊ณผ) |
| 14 | + * TC: O(w^s * wordDict ๋จ์ด์ ๊ธธ์ด), SC: O(s) |
| 15 | + */ |
| 16 | + private fun usingDFS(s: String, wordDict: List<String>): Boolean { |
| 17 | + |
| 18 | + fun recursion(s: String, wordDict: List<String>, index: Int): Boolean = |
| 19 | + if (index == s.length) true |
| 20 | + else { |
| 21 | + wordDict.map { word -> |
| 22 | + var result = false |
| 23 | + if (index + word.length < s.length + 1 && s.substring(index, index + word.length) == word) { |
| 24 | + result = recursion(s, wordDict, index + word.length) |
| 25 | + } |
| 26 | + result |
| 27 | + }.find { it } ?: false |
| 28 | + } |
| 29 | + |
| 30 | + return recursion(s, wordDict, 0) |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * 2, ์ฌ์ ์ ๋ด๊ฒจ์๋ ๋ฌธ์์ด๋ค์ ๊ธฐ์ค์ผ๋ก ์ธ๋ฑ์ค๋ฅผ ์ฆ๊ฐ์ํค๋ฉด์ ๋ฌธ์์ด์ ์์ฑ์ํจ๋ค. ํ ๋ฒ ํ์ํ์ฌ ๋ฌธ์์ด์ ์์ฑ์ํค์ง ๋ชปํ ์ธ๋ฑ์ค๋ฅผ ์ ์ฅํ์ฌ ํด๋น ์ธ๋ฑ์ค๋ ๋ค์ ํ์ํ์ง ์๋๋ก ํ์ฌ ์ฑ๋ฅ์ ๊ฐ์ ํ๋ค. |
| 35 | + * TC: O(s * w * wordDict ๋จ์ด์ ๊ธธ์ด), SC: O(s) |
| 36 | + */ |
| 37 | + private fun usingMemoizationDFS(s: String, wordDict: List<String>): Boolean{ |
| 38 | + |
| 39 | + fun dfs(s: String, wordDict: List<String>, index: Int, memo: MutableSet<Int>): Boolean { |
| 40 | + val len = s.length |
| 41 | + if(index == len) return true |
| 42 | + else if(memo.contains(index)) return false |
| 43 | + |
| 44 | + for (word in wordDict) { |
| 45 | + if (index + word.length < s.length + 1 && |
| 46 | + s.substring(index, index + word.length) == word && |
| 47 | + dfs(s, wordDict, index + word.length, memo)) { |
| 48 | + return true |
| 49 | + } |
| 50 | + } |
| 51 | + memo.add(index) |
| 52 | + return false |
| 53 | + } |
| 54 | + |
| 55 | + if(s.isEmpty()) return false |
| 56 | + return dfs(s, wordDict, 0, mutableSetOf()) |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * 3. ๋ฌธ์์ด์ ๋๋ถํฐ 0๊น์ง ์ํํ๋ฉด์ ์ํํ๋ ๋ฒ์์ ๋ฌธ์์ด์ ๋ง๋ค ์ ์๋ค๋ฉด ํด๋น ์ธ๋ฑ์ค๋ฅผ true๋ก ๋ณํํ์ฌ ์ด์ ์ ์ฌ์ฉํ ์ฐ์ฐ์ ๊ฒฐ๊ณผ๋ฅผ ์ฌํ์ฉํ๋ค. |
| 61 | + * TC: O(s * w * wordDict ๋จ์ด์ ๊ธธ์ด) SC: O(s) |
| 62 | + */ |
| 63 | + private fun usingDP(s: String, wordDict: List<String>): Boolean { |
| 64 | + val dp = BooleanArray(s.length + 1).apply { |
| 65 | + this[s.length] = true |
| 66 | + } |
| 67 | + |
| 68 | + for (index in s.length - 1 downTo 0) { |
| 69 | + for (word in wordDict) { |
| 70 | + if (dp[index]) break |
| 71 | + else if (index + word.length <= s.length && s.substring(index, index + word.length) == word) { |
| 72 | + dp[index] = dp[index + word.length] |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + return dp[0] |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + fun `๋ฌธ์์ด๊ณผ ๋ฌธ์์ด ์ฌ์ ์ด ์ฃผ์ด์ก์ ๋ ๋ฌธ์์ด ์ฌ์ ์ ์ด์ฉํ์ฌ ๋ฌธ์์ด์ ์์ฑํ ์ ์์ผ๋ฉด ์ฐธ์ ๋ฐํํ๋ค`() { |
| 81 | + wordBreak("applepenapple", listOf("apple", "pen")) shouldBe true |
| 82 | + wordBreak("leetcode", listOf("leet", "co", "de")) shouldBe true |
| 83 | + wordBreak("abcd", listOf("a","abc","b","cd")) shouldBe true |
| 84 | + wordBreak("cars", listOf("car","ca","rs")) shouldBe true |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + fun `๋ฌธ์์ด๊ณผ ๋ฌธ์์ด ์ฌ์ ์ด ์ฃผ์ด์ก์ ๋ ๋ฌธ์์ด ์ฌ์ ์ ์ด์ฉํ์ฌ ๋ฌธ์์ด์ ์์ฑํ ์ ์๋ค๋ฉด ๊ฑฐ์ง์ ๋ฐํํ๋ค`() { |
| 89 | + wordBreak("catsandog", listOf("cats", "dog", "sand", "and", "cat")) shouldBe false |
| 90 | + } |
| 91 | +} |
0 commit comments