[Swift]LeetCode488. 祖瑪遊戲 | Zuma Game
Think about Zuma Game. You have a row of balls on the table, colored red(R), yellow(Y), blue(B), green(G), and white(W). You also have several balls in your hand.
Each time, you may choose a ball in your hand, and insert it into the row (including the leftmost place and rightmost place). Then, if there is a group of 3 or more balls in the same color touching, remove these balls. Keep doing this until no more balls can be removed.
Find the minimal balls you have to insert to remove all the balls on the table. If you cannot remove all the balls, output -1.
Examples:
Input: "WRRBBW", "RB"
Output: -1
Explanation: WRRBBW -> WRR[R]BBW -> WBBW -> WBB[B]W -> WW
Input: "WWRRBBWW", "WRBRW"
Output: 2
Explanation: WWRRBBWW -> WWRR[R]BBWW -> WWBBWW -> WWBB[B]WW -> WWWW -> empty
Input:"G", "GGGGG"
Output: 2
Explanation: G -> G[G] -> GG[G] -> empty
Input: "RBYYBBRRB", "YRBGB"
Output: 3
Explanation: RBYYBBRRB -> RBYY[Y]BBRRB -> RBBBRRB -> RRRB -> B -> B[B] -> BB[B] -> empty
Note:
- You may assume that the initial row of balls on the table won’t have any 3 or more consecutive balls with the same color.
- The number of balls on the table won‘t exceed 20, and the string represents these balls is called "board" in the input.
- The number of balls in your hand won‘t exceed 5, and the string represents these balls is called "hand" in the input.
- Both input strings will be non-empty and only contain characters ‘R‘,‘Y‘,‘B‘,‘G‘,‘W‘.
回憶一下祖瑪遊戲。現在桌上有一串球,顏色有紅色(R),黃色(Y),藍色(B),綠色(G),還有白色(W)。 現在你手裏也有幾個球。
每一次,你可以從手裏的球選一個,然後把這個球插入到一串球中的某個位置上(包括最左端,最右端)。接著,如果有出現三個或者三個以上顏色相同的球相連的話,就把它們移除掉。重復這一步驟直到桌上所有的球都被移除。
找到插入並可以移除掉桌上所有球所需的最少的球數。如果不能移除桌上所有的球,輸出 -1 。
示例: 輸入: "WRRBBW", "RB" 輸出: -1 解釋: WRRBBW -> WRR[R]BBW -> WBBW -> WBB[B]W -> WW (翻譯者標註:手上球已經用完,桌上還剩兩個球無法消除,返回-1) 輸入: "WWRRBBWW", "WRBRW" 輸出: 2 解釋: WWRRBBWW -> WWRR[R]BBWW -> WWBBWW -> WWBB[B]WW -> WWWW -> empty 輸入:"G", "GGGGG" 輸出: 2 解釋: G -> G[G] -> GG[G] -> empty 輸入: "RBYYBBRRB", "YRBGB" 輸出: 3 解釋: RBYYBBRRB -> RBYY[Y]BBRRB -> RBBBRRB -> RRRB -> B -> B[B] -> BB[B] -> empty
標註:
- 你可以假設桌上一開始的球中,不會有三個及三個以上顏色相同且連著的球。
- 桌上的球不會超過20個,輸入的數據中代表這些球的字符串的名字是 "board" 。
- 你手中的球不會超過5個,輸入的數據中代表這些球的字符串的名字是 "hand"。
- 輸入的兩個字符串均為非空字符串,且只包含字符 ‘R‘,‘Y‘,‘B‘,‘G‘,‘W‘。
Runtime: 1004 ms Memory Usage: 4 MB
1 class Solution { 2 func findMinStep(_ board: String, _ hand: String) -> Int { 3 var res:Int = Int.max 4 var s:Set<Character> = Set<Character>() 5 for i in 0..<hand.count 6 { 7 if s.contains(hand[i]) {continue} 8 s.insert(hand[i]) 9 for j in 0..<board.count 10 { 11 if board[j] != hand[i] {continue} 12 var newBoard = board 13 var newHand = hand 14 let index1 = newBoard.index(newBoard.startIndex, offsetBy: j) 15 newBoard.insert(hand[i],at:index1) 16 newBoard = removeConsecutive(newBoard) 17 if newBoard.count == 0 {return 1} 18 let index2 = newHand.index(newHand.startIndex, offsetBy: i) 19 newHand.remove(at:index2) 20 var cnt:Int = findMinStep(newBoard, newHand) 21 if cnt != -1 22 { 23 res = min(res, cnt + 1) 24 } 25 } 26 } 27 return res == Int.max ? -1 : res 28 } 29 30 func removeConsecutive(_ board:String) -> String 31 { 32 var j:Int = 0 33 for i in 0...board.count 34 { 35 if i < board.count && board[i] == board[j] {continue} 36 if i - j >= 3 {return removeConsecutive(board.subString(0, j) + board.subString(i))} 37 else{j = i} 38 } 39 return board 40 } 41 } 42 43 extension String { 44 //subscript函數可以檢索數組中的值 45 //直接按照索引方式截取指定索引的字符 46 subscript (_ i: Int) -> Character { 47 //讀取字符 48 get {return self[index(startIndex, offsetBy: i)]} 49 } 50 51 // 截取字符串:指定索引和字符數 52 // - begin: 開始截取處索引 53 // - count: 截取的字符數量 54 func subString(_ begin:Int,_ count:Int) -> String { 55 let start = self.index(self.startIndex, offsetBy: max(0, begin)) 56 let end = self.index(self.startIndex, offsetBy: min(self.count, begin + count)) 57 return String(self[start..<end]) 58 } 59 60 // 截取字符串:從index到結束處 61 // - Parameter index: 開始索引 62 // - Returns: 子字符串 63 func subString(_ index: Int) -> String { 64 let theIndex = self.index(self.endIndex, offsetBy: index - self.count) 65 return String(self[theIndex..<endIndex]) 66 } 67 }
[Swift]LeetCode488. 祖瑪遊戲 | Zuma Game