Swift solution - DFS, Stack


  • 0
    class Solution {
        func decodeString(_ s: String) -> String {
            var pos = 0
            return DFS(Array(s.characters), &pos)
        }
        
        private func DFS(_ characters: [Character], _ pos: inout Int) -> String {
            var result = ""
            
            while pos < characters.count && characters[pos] != "]" {
                if !isDigit(characters[pos]) {
                    result += String(characters[pos])
                    pos += 1
                } else {
                    var n = 0
                    while pos < characters.count && isDigit(characters[pos]) {
                        n = n * 10 + String(characters[pos]).asciiValueOfCharacter - "0".asciiValueOfCharacter
                        pos += 1
                    }
                    pos += 1
                    let str = DFS(characters, &pos)
                    pos += 1
                    while n > 0 {
                        result += str
                        n -= 1
                    }
                }
            }
            
            return result
        }
        
        private func isDigit(_ character: Character) -> Bool {
            guard let _ = Int(String(character)) else {
                return false
            }
            return true
        }
        
        func decodeString_Stack(_ s: String) -> String {
            var count = [Int]()
            var result = [""]
            var characters = Array(s.characters)
            var i = 0
            
            while i < characters.count {
                let character = String(characters[i])
                if character.asciiValueOfCharacter >= "0".asciiValueOfCharacter && character.asciiValueOfCharacter <= "9".asciiValueOfCharacter {
                    let start = i
                    while String(characters[i + 1]).asciiValueOfCharacter >= "0".asciiValueOfCharacter && String(characters[i + 1]).asciiValueOfCharacter <= "9".asciiValueOfCharacter {
                        i += 1
                    }
                    count.append(Int(String(characters[start...i]))!)
                } else if character == "[" {
                    result.append("")
                } else if character == "]" {
                    let str = result.removeLast()
                    var strs = [String]()
                    for _ in 0..<count.removeLast() {
                        strs.append(str)
                    }
                    result.append(result.removeLast() + strs.joined())
                } else {
                    result.append(result.removeLast() + character)
                }
                i += 1
            }
            
            return result.removeLast()
        }
    }
    
    extension String {
        var asciiValueOfCharacter: Int {
            get {
                let value = self.unicodeScalars.filter{$0.isASCII}.first?.value ?? 0
                return Int(value) 
            }
        }
    }
    

Log in to reply
 

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