Swift Solution


  • 0
    R

    Here is my solution in Swift. It is basically the same as others posted here in Java and other languages. However, this solution is not approved due to it taking too much time with an input of 30. Has anyone gotten a solution working in swift?

        var s = "1"
    
        for _ in 1 ..< n {
    
            var newS = ""
            var count = 1
    
            for j in 1 ... s.count {
    
                if j == s.count || s[j-1] != s[j] {
                    newS += "\(count)\(s[j-1])"
                    count = 1
                } else { count += 1 }
    
            }
    
            s = newS
        }
    
        return s
    

Log in to reply
 

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