|
| 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(s^w * 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 단어의 길이) TC: 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