美文网首页
剑指 Offer II 108. 单词演变

剑指 Offer II 108. 单词演变

作者: 邦_ | 来源:发表于2022-08-15 10:53 被阅读0次

    感觉结果是对的。。 但是超时了= =。。

    
    func ladderLength(_ beginWord: String, _ endWord: String, _ wordList: [String]) -> Int {
    
            if !wordList.contains(endWord) {
                return 0
            }
            var wordSet :Set<String> = Set.init(wordList)
            var tempArray = [String]()
            tempArray.append(beginWord)
            var count = 1
            while tempArray.count > 0 {
                
                let len = tempArray.count
                
                for _ in 0..<len{
                    
                    let cur = tempArray.removeLast()
                    if cur == endWord {
                        return count
                    }
                    for w in wordSet {
                        if canChange(w, cur) {
                            wordSet.remove(w)
                            tempArray.insert(w, at: 0)
                        }
                    }
                    
                }
            
                count += 1
    
            }
       
            return 0
        
        
        }
    
        func canChange(_ v1:String, _ v2:String) -> Bool {
            let array1 = Array(v1)
            let array2 = Array(v2)
            let len = array1.count
            var count = 0
            for i in 0..<len {
                
                if array1[i] != array2[i] {
                    count += 1
                }
                if count > 1 {
                    return false
                }
                
            }
            return true
        }
    
    

    换种写法 a的ascil码 97 可能这样写加快了转换速度= =。。

    
    
     func ladderLength(_ beginWord: String, _ endWord: String, _ wordList: [String]) -> Int {
    
            if !wordList.contains(endWord) {
                return 0
            }
            var wordSet :Set<String> = Set.init(wordList)
            var tempArray = [String]()
            tempArray.append(beginWord)
            var count = 1
            while tempArray.count > 0 {
                
                let len = tempArray.count
                
                for _ in 0..<len{
                    
                    let cur = tempArray.removeLast()
                    if cur == endWord {
                        return count
                    }
                    for w in getNext(cur) {
                        if wordSet.contains(w){
                            wordSet.remove(w)
                            tempArray.insert(w, at: 0)
                        }
                    }
                    
                }
            
                count += 1
    
            }
      
            return 0
        
        
        }
      
        
        
        func getNext(_ word:String) -> [String] {
            var arr = Array(word)
            var res = [String]()
            let len = arr.count
    
            for i in 0..<len {
                let temp = arr[i]
                for j in 0..<26 {
                    arr[i] = Character(UnicodeScalar(97 + j)!)
                    if temp != arr[i] {
                        res.append(String(arr))
                    }
                }
                //还原回去
                arr[i] = temp
            }
            
            return res
            
        }
    
    
    
    

    相关文章

      网友评论

          本文标题:剑指 Offer II 108. 单词演变

          本文链接:https://www.haomeiwen.com/subject/hahtgrtx.html