80 Lines Brute-force Swift Solution


  • 0
    Y
    class Solution {
       func findMinStep(_ board: String, _ hand: String) -> Int {
            var handRecord:[Character:Int] = [:]
            var res = 6
            for char in hand.characters{
                if handRecord[char] == nil{
                    handRecord.updateValue(1, forKey: char)
                }else{
                    handRecord[char]! += 1
                }
            }
            helper(board: board, hand: &handRecord, numsOfHand: hand.characters.count, res: &res, cur: 0)
            return res == 6 ? -1 : res    
        }
        
        func helper(board:String, hand:inout [Character:Int], numsOfHand:Int, res: inout Int, cur:Int){
            if res<=0 || cur > numsOfHand{
                return
            }
            var i = 0
            let boardRecord = board.characters.map({ String($0) })
            while i<boardRecord.count{
                if i+1<boardRecord.count && boardRecord[i]==boardRecord[i+1]{
                    if hand[Character(boardRecord[i])] != nil && hand[Character(boardRecord[i])]! > 0{
                        hand[Character(boardRecord[i])]! -= 1
                        var nextBoard = boardRecord
                        nextBoard.insert(boardRecord[i], at: i)
                        nextBoard = refreshBoard(board: nextBoard)
                        if nextBoard.count > 0{
                            helper(board: array2String(array: nextBoard), hand: &hand, numsOfHand: numsOfHand, res: &res, cur: cur+1)
                        }else{
                            res = min(res, cur + 1)
                        }
                        hand[Character(boardRecord[i])]! += 1
                        i+=1
                    }
                }else{
                    if hand[Character(boardRecord[i])] != nil && hand[Character(boardRecord[i])]!>=2{
                        hand[Character(boardRecord[i])]! -= 2
                        var nextBoard = boardRecord
                        nextBoard.insert(boardRecord[i], at: i)
                        nextBoard.insert(boardRecord[i], at: i)
                        nextBoard = refreshBoard(board: nextBoard)
                        if nextBoard.count > 0{
                            helper(board: array2String(array: nextBoard), hand: &hand, numsOfHand: numsOfHand, res: &res, cur: cur+2)
                        }else{
                            res = min(res, cur + 2)
                        }
                        hand[Character(boardRecord[i])]! += 2     
                    }
                }
                i+=1
            }    
        }
        func refreshBoard(board:[String]) -> [String]{
            var boardRecord = board
            var count = 0
            for i in 0..<boardRecord.count{
                if i+1<boardRecord.count && boardRecord[i] == boardRecord[i+1]{
                    count+=1
                }else{
                    if count>=2{
                        for _ in 0..<count+1{
                            boardRecord.remove(at: i-count)
                        }
                        return refreshBoard(board:boardRecord)
                    }
                    count = 0
                }
            }
            return boardRecord
        }    
        func array2String(array:[String])->String{
            var string:String = ""  
            for i in array{
                string.append(i)
            }
            return string        
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.