|
| 1 | +package leetcode_study |
| 2 | + |
| 3 | +import io.kotest.matchers.shouldBe |
| 4 | +import leetcode_study.`word-search`.Position.Companion.MOVES |
| 5 | +import org.junit.jupiter.api.Test |
| 6 | + |
| 7 | +class `word-search` { |
| 8 | + |
| 9 | + /** |
| 10 | + * 격자에 존재하는 문자를 사용하여 word 를 만들 수 있는지 확인하기 위해 DFS를 통한 visited 배열 백트래킹을 사용하여 해결 |
| 11 | + * TC: O(너비 * 높이 * 4^word), SC: O(너비 * 높이 * 4^word) |
| 12 | + */ |
| 13 | + fun exist(board: Array<CharArray>, word: String): Boolean { |
| 14 | + return usingBacktracking(board, word) |
| 15 | + } |
| 16 | + |
| 17 | + private fun usingBacktracking(board: Array<CharArray>, word: String): Boolean { |
| 18 | + fun dfs(board: Array<CharArray>, visited: Array<BooleanArray>, word: String, position: Position, index: Int): Boolean { |
| 19 | + if (index == word.length) return true |
| 20 | + for (move in MOVES) { |
| 21 | + val next = position + move |
| 22 | + if (next.isNotOutOfIndexed(board) && !visited[next.x][next.y] && board[next.x][next.y] == word[index]) { |
| 23 | + visited[next.x][next.y] = true |
| 24 | + if (dfs(board, visited, word, next, index + 1)) return true |
| 25 | + visited[next.x][next.y] = false |
| 26 | + } |
| 27 | + } |
| 28 | + return false |
| 29 | + } |
| 30 | + |
| 31 | + val visited = Array(board.size) { |
| 32 | + BooleanArray(board[0].size) |
| 33 | + } |
| 34 | + |
| 35 | + for (x in board.indices) { |
| 36 | + for (y in board[x].indices) { |
| 37 | + visited[x][y] = true |
| 38 | + if (board[x][y] == word[0] && dfs(board, visited, word, Position(x,y), 1)) { |
| 39 | + return true |
| 40 | + } |
| 41 | + visited[x][y] = false |
| 42 | + } |
| 43 | + } |
| 44 | + return false |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + fun `문자로 구성된 2차원 배열에서 word 문자열 존재 유무를 반환한다`() { |
| 49 | + exist(arrayOf( |
| 50 | + charArrayOf('A','B','C','E'), |
| 51 | + charArrayOf('S','F','C','S'), |
| 52 | + charArrayOf('A','D','E','E') |
| 53 | + ), "ABCCED") shouldBe true |
| 54 | + exist(arrayOf( |
| 55 | + charArrayOf('A','B','C','E'), |
| 56 | + charArrayOf('S','F','C','S'), |
| 57 | + charArrayOf('A','D','E','E') |
| 58 | + ), "SEE") shouldBe true |
| 59 | + exist(arrayOf( |
| 60 | + charArrayOf('A','B','C','E'), |
| 61 | + charArrayOf('S','F','C','S'), |
| 62 | + charArrayOf('A','D','E','E') |
| 63 | + ), "SES") shouldBe false |
| 64 | + exist(arrayOf( |
| 65 | + charArrayOf('A','B','C','E'), |
| 66 | + charArrayOf('S','F','E','S'), |
| 67 | + charArrayOf('A','D','E','E') |
| 68 | + ), "ABCESEEEFS") shouldBe true |
| 69 | + exist(arrayOf( |
| 70 | + charArrayOf('C','A','A'), |
| 71 | + charArrayOf('A','A','A'), |
| 72 | + charArrayOf('B','C','D') |
| 73 | + ), "AAB") shouldBe true |
| 74 | + } |
| 75 | + |
| 76 | + data class Position( |
| 77 | + val x: Int, |
| 78 | + val y: Int |
| 79 | + ) { |
| 80 | + |
| 81 | + operator fun plus(other: Position) = Position(this.x + other.x, this.y + other.y) |
| 82 | + |
| 83 | + fun isNotOutOfIndexed(board: Array<CharArray>) = |
| 84 | + this.x < board.size && this.x >= 0 && this.y < board[0].size && this.y >= 0 |
| 85 | + |
| 86 | + companion object { |
| 87 | + val MOVES: List<Position> = listOf( |
| 88 | + Position(-1, 0), |
| 89 | + Position(0, 1), |
| 90 | + Position(1, 0), |
| 91 | + Position(0, -1), |
| 92 | + ) |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments