美文网首页
844. 比较含退格的字符串

844. 比较含退格的字符串

作者: 邦_ | 来源:发表于2022-08-02 09:13 被阅读0次
    
    func backspaceCompare(_ s: String, _ t: String) -> Bool {
    
            let arrayS = Array(s) , arrayT = Array(t)
            var m = arrayS.count - 1 ,n =  arrayT.count - 1
            var tempM = 0 ,tempN = 0 ,valueM :Character = " " ,valueN :Character = " "
            while true  {
                
                if m >= 0 {
                    valueM = arrayS[m]
                }else {
                    valueM = " "
                }
                
                if n >= 0 {
                    valueN = arrayT[n]
                }else {
                    valueN = " "
                }
    
                if valueM == " " && valueN == " " {
                
                    return true
                }
                
                if valueM == "#"{
                    tempM += 1
                    m -= 1
                    
                }else if valueN == "#"{
                    tempN += 1
                    n -= 1
    
                }
                else if tempM != 0 {
                    
                    m -= 1
                    tempM -= 1
                    
                }else if tempN != 0 {
                    
                    n -= 1
                    tempN -= 1
                    
                }
                else if valueM == valueN {
                    m -= 1
                    n -= 1
                }
                else if valueM != valueN {
                    
                    return false
                
                }
                
            }
    
        
            
        }
    
    
    
    
    
    
    
    
    
    
    
    
    

    相关文章

      网友评论

          本文标题:844. 比较含退格的字符串

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