★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10348594.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
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'。
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 }